Click here to Skip to main content
15,886,030 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a GridView, one of which collumn has been transformed to TemplateField that likes this:
XML
<asp:TemplateField HeaderText="ID" SortExpression="id">
    <EditItemTemplate>
        <asp:TextBox ID="TextBox_Edit_ID" runat="server" Text='<%# Bind("id") %>' ></asp:TextBox>
    </EditItemTemplate>
    <ItemTemplate>
        <asp:Label ID="Label_Edit_ID" runat="server" Text='<%# Bind("id") %>'></asp:Label>
    </ItemTemplate>
</asp:TemplateField>

I have found the TextBox control in GridView's RowUpdating event by the code that likes this
C#
protected void GridView_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
TextBox tb = (TextBox)GridView.Rows[e.RowIndex].FindControl("TextBox_Edit_Name");
//other code here......
}

But I could not find the Label control "Label_Edit_ID" by similar code, for example:
C#
protected void GridView_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
TextBox tb = (TextBox)GridView.Rows[e.RowIndex].FindControl("TextBox_Edit_Name");
Label lbl = (Label)GridView.Rows[e.RowIndex].FindControl("Label_Edit_Name");
//other code here......
}

Everytime the code runs, it meets an error showing that the lbl is null. Is it impossible to find control in ItemTemplate ?
Posted
Updated 23-Oct-12 4:48am
v3

XML
Change Code Like this then try to find label

<asp:TemplateField HeaderText="ID" SortExpression="id">
    <EditItemTemplate>
        <asp:TextBox ID="TextBox_Edit_ID" runat="server" Text='<%# Bind("id") %>' >
<asp:Label ID="Label_Edit_ID" runat="server" Text='<%# Bind("id") %>'></asp:TextBox>
    </EditItemTemplate>
    <ItemTemplate>
        <asp:Label ID="Label_items_ID" runat="server" Text='<%# Bind("id") %>'></asp:Label>
    </ItemTemplate>
</asp:TemplateField>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900