2012-10-11 114 views
0

我新的ASP.NETASP.NET - Detech標籤文本傳遞變量

我嘗試做的是捕捉到lblProposedID領域的唯一ID(ProposedID),並把它傳遞到另一個頁面。

拉布勒在GridView

<asp:TemplateField HeaderText="ID" 
    SortExpression="ID" ItemStyle-Width="5%"> 
    <ItemTemplate> 
    <asp:Label ID="lblProposedID" runat="server" 
     Text='<%#Eval("ProposedID") %>'> 
    </asp:Label> 
    </ItemTemplate> 
    <FooterTemplate> 
    </FooterTemplate> 
</asp:TemplateField> 

代碼隱藏

private void GridView1_RowCreated(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e) 
{ 
    String rColor; 
    System.Drawing.ColorConverter colConvert = new ColorConverter(); 

    if (e.Row.RowType == DataControlRowType.DataRow) 
    { 
     int RowNum = e.Row.RowIndex; 
     if (RowNum % 2 == 1) 
     { 
      rColor = "#FFFFFF"; 
      //e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='#DDDDDD'"); 
     } 
     else 
     { 
      rColor = "#F5F5F5"; 
      //e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='" ++ "'"); 
     } 

     e.Row.BackColor = (System.Drawing.Color)colConvert.ConvertFromString(rColor); 
     e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='" + rColor + "'"); 
     e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='#00FFFF'"); 
     e.Row.Attributes.Add("onclick", "window.open('popup.aspx?ProposedID=" + (Label) e.Row.FindControl("lblProposedID") + 
     "','cal','width=600,height=300,left=270,top=180')"); 
     //e.Row.Attributes["onclick"] = this.Page.ClientScript.GetPostBackClientHyperlink(this.GridView1, "Select$" + e.Row.RowIndex); 
    } 
} 

這是當我無法得到它的工作就行了。

e.Row.Attributes.Add("onclick", "window.open('popup.aspx?textbox={0}" + (Label)e.Row.FindControl("lblProposedID") + 
"','cal','width=600,height=300,left=270,top=180')"); 

請幫忙。提前致謝。

回答

0

改爲使用OnRowDataBound。該文本總是在這種情況下空,但是當我把它改成的RowDataBound,它選擇了ID

添加RowDataBound事件

<asp:GridView runat="server" ID="GridView1" 
AutoGenerateColumns="false" OnRowDataBound="GridView1_RowCreated"> 

代碼隱藏

protected void GridView1_RowDataBound(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e) 
    { 
     String rColor; 

     if (e.Row.RowType == DataControlRowType.DataRow) 
     { 
      int RowNum = e.Row.RowIndex; 
      if (RowNum % 2 == 1) 
      { 
       rColor = "#FFFFFF"; 
       //e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='#DDDDDD'"); 
      } 
      else 
      { 
       rColor = "#F5F5F5"; 
       //e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='" ++ "'"); 
      } 

      e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='" + rColor + "'"); 
      e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='#00FFFF'"); 
      e.Row.Attributes.Add("onclick", "window.open('popup.aspx?ProposedID=" + ((Label)e.Row.FindControl("lblProposedID")).Text + 
      "','cal','width=600,height=300,left=270,top=180')"); 
      //e.Row.Attributes["onclick"] = this.Page.ClientScript.GetPostBackClientHyperlink(this.GridView1, "Select$" + e.Row.RowIndex); 
     } 
    } 
+0

非常感謝,它在OnRowDataBound – Milacay

+0

中有效很高興爲您提供幫助。 – codingbiz

1

你需要改變你如何讓JS行這項工作的一部分:

((Label)e.Row.FindControl("lblProposedID")).Text

這將讓從標籤的實際文本。

+0

我試過了,但它只是傳遞一無所有。我甚至試過這個「((標籤)e.Row.FindControl(」lblProposedID「))。Text.ToString()」,沒有運氣。有什麼建議麼?謝謝! – Milacay