Click here to Skip to main content
15,892,674 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have a grid like this...

XML
<asp:GridView ID="gdv" runat="server">
            <Columns>
                <asp:TemplateField>
                    <HeaderTemplate>
                        <asp:TextBox ID="txt" runat="server"></asp:TextBox>
                    </HeaderTemplate>
                    <ItemTemplate>
                        <asp:Label ID="lbl" runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "col1") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField>
                    <ItemTemplate>
                        <asp:DropDownList ID="ddl" runat="server" DataTextField="item" DataValueField="value"></asp:DropDownList>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>





How can find the TextBox control which is there inside one of the header template in code behind(C# code) ?
Posted
Updated 7-Jul-16 2:14am

By handling RowDataBound() event of the gridview you can find Textbox control inside header template.

C#
protected void gdv_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (gdv.HeaderRow != null)
        {
            TextBox txt = (TextBox)gdv.HeaderRow.FindControl("txt");
        }
    }
 
Share this answer
 
C#
protected void chkSelectAll_CheckedChanged(object sender, EventArgs e)
    {
        CheckBox chk;

        foreach (GridViewRow rowItem in GridView1.Rows)
        {
            chk = (CheckBox)(rowItem.Cells[0].FindControl("chk1″));
            chk.Checked =((CheckBox)sender).Checked;
        }
    }
 
Share this answer
 
v2
Comments
Arun Jacob 13-Jul-10 8:44am    
If you use formatting post will be more readable.
Raghavendra HG 13-Jul-10 9:21am    
This wont work dude as you are finding the control inside the Rows.
Here's what worked for us:

private void Lab_1_GV1_Populate_SearchText()
{
GridView GV1 = (GridView)FindControl("Lab_1_GV1");
TextBox TXB1 = (TextBox)GV1.HeaderRow.FindControl("Lab_1_TX2GV1");
}
 
Share this answer
 
v2
Comments
[no name] 23-Aug-13 7:39am    
The question was asked and answered over 3 years ago. There is no real need to answer it again.

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