c#
  • asp.net
  • 2016-09-21 38 views 0 likes 
    0

    我有一個標籤,在Itemtemplate中,我想獲得標籤的值,但 只是找不到控件的標籤,請有人幫助我?謝謝如何獲取標籤位於ItemTemplate中C#代碼後面的標籤?

    ASPX代碼:

    <asp:TemplateField HeaderText="工號" SortExpression="BS_ID"> <ItemTemplate> <asp:Label ID="lblBSID" runat="server" Text='<%# Eval("BS_ID") %>' /> </ItemTemplate> <EditItemTemplate> <asp:Textbox ID="tbBS_ID_edit" runat="server" Text='<%# Eval("BS_ID") %>' /> </EditItemTemplate> <FooterTemplate> <asp:TextBox ID="BS_ID_tb" Text="SK" MaxLength="7" runat="server" /> </FooterTemplate> </asp:TemplateField> 代碼

    1. 這是我VAR

    //string lb_BS_ID = ((Label)GridView1.FindControl("lblBSID")).Text; //this._userID = lb_BS_ID;

  • ,我也嘗試
  • //string lb_BS_ID = ((Label)GridView1.Rows[0].FindControl("lblBSID")).Text; //this._userID = lb_BS_ID;

  • 此外
  • //string lb_BS_ID =((Label)GridView1.Rows[e.RowIndex].Cells[0]FindControl("lblBSID")).Text; //this._userID = lb_BS_ID;

    但這些都是不適合我,請幫助我工作> <

    回答

    0

    的這個: string lb_BS_ID = ((Label)GridView1.FindControl("lblBSID")).Text;

    試試這個。 :

    Lable label = (Label)GridView1.Rows[e.RowIndex].FindControl("lblBSID"); 
    
    string value = label.Text; 
    

    注意:如果你只需要找到在第一行的標籤的值,然後使用和e.RowIndex等等,而不是[0]行。 我希望它有幫助。

    +0

    對不起兄弟你不能訪問任何CNTROL,我忘了說我已經嘗試過了......而且這不起作用T_T – Woody

    +0

    你介意給我回答一個問題嗎?當我開始我var一個私人字符串_userID,我通過文本框插入數據到數據庫後,_userID值變爲null,這是正常的? – Woody

    +0

    如果是正常的,在插入數據庫之後如何存儲_userID之類的值?@@ – Woody

    0

    用於更新記錄使用gridview的「OnRowUpdating」事件; 同時使用您的代碼1,2和3確保您的網格綁定到某些數據。

    ,直到你的網格綁定到一些數據

    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e) 
          { 
           int trackingno = (int)GridView1.DataKeys[e.RowIndex].Value; 
           Label trackingno = (Label)GridView1.Rows[e.RowIndex].FindControl("yourLabelControlID"); 
    //perform your update function 
        } 
    

    你可以使用其他業務「OnRowDataBound」

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) 
         { 
    
           DropDownList ddeditpickuprider = (DropDownList)e.Row.FindControl("yourDropdownControlID"); 
    } 
    
    相關問題