ASP.Net C# GridView FindControl Checkbox

Updated on 03 Aug 2012,
Published on 30 Aug 2008

ASP.Net GridView FindControl function can be used to access the Checkbox control placed inside the ItemTemplate of TemplateField Column. FindControl function returns the naming container of the server control (e.g. checkbox in this tutorial) who’s ID is passed to the function. ASP.Net GridView FindControl function searches the server control within the row items and column of the GridView control. In this tutorial we will discuss how to find checkbox control placed inside the ASP.Net GridView also you can access the properties of checkbox control such as Text property or checkbox state property i.e. Checked.

HTML Code for ASP.Net GridView Checkbox

<asp:GridView ID="GridView1" 
    runat="server" 
    AutoGenerateColumns="False" 
    BorderStyle="Solid"
    CellPadding="4" 
    DataKeyNames="CategoryID" 
    BorderColor="Silver" 
    BorderWidth="1px"
    Width="300px">
    <Columns>
        <asp:TemplateField HeaderText="Categories">
            <ItemTemplate>
                <asp:CheckBox ID="chk1" 
                    runat="server" 
                    Text='<%#DataBinder.Eval(Container.DataItem,"categoryID") + " " + DataBinder.Eval(Container.DataItem,"categoryName") %>' />
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
    <HeaderStyle HorizontalAlign="Left" />
</asp:GridView>

ASP.Net GridView FindControl Checkbox C# Code

CheckBox chk;

foreach (GridViewRow rowItem in GridView1.Rows)
{
    // FindControl function gets the control placed inside the GridView control from the specified cell
    // FindControl fucntion accepts string id of the control that you want to access
    // type casting of control allows to access the properties of that particular control
    // here checkbox control type cast is used to access its properties
    chk = (CheckBox)(rowItem.Cells[0].FindControl("chk1"));


    // chk.checked will access the checkbox state on button click event
    if (chk.Checked)
    {
        Response.Write(chk.ClientID + "<br />");
    }
}

Above C# code shows the use of ASP.Net GridView FindControl Checkbox to access the Checked property of checkbox placed inside the GridView control. C# foreach loop has been used to access the checkbox inside each row of the GridView control. rowItem object of GridViewRow Class enables you to access the instance of a particular row and its Cells (Columns). You can access any particular cell by passing the number value such as 0, 1 or 2 to access 1st, 2nd or 3rd column within the limit of number of columns in each row. If there are 3 columns in each row of GridView then you can access each cell by passing 0 for 1st column, 1 for 2nd column and 2 for 3rd column. In the given sample code you can see that we have placed a checkbox control inside the first column of GridView control that is why we have used rowItem.Cells[0].FindControl( "chk1" ) to access the 1st cell of each row. "chk1" is passed as string id parameter of FindControl function to access the checkbox control by ID.

Output:

Get the output of above discussed code from the following link:

GridView FindControl Checkbox

Continue reading to the next tutorial ASP.Net GridView Checkbox Select All using C# to learn how to select all the checkbox controls placed inside the GridView control with a single click.

10 Responses to "ASP.Net C# GridView FindControl Checkbox"
This code is very help full to me.
Tom
Thank you for a simple easy to follow example!!!
gayatri
thanks a lot for giving a simple solution
Mohammad
thank you so much for giving such simple example
Stephen
Thank you, you saved my skin. I cant believe how daft I was being.
vinod
good link
See this link. Let me know if this helps.

http://www.z5concepts.com/blog/post/C-ASPNET-Use-FindControl-on-TemplateField-ItemTemplate.aspx
Thanks good article
Moahmmad
Tahnks alot :)
Sohrab
Very thanks...
Leave a Comment
* required
* required
* will not be published
* optional
* hint: http://www.example.com
  • Subscribe via Email