GridView FindControl Checkbox using VB.Net

Updated on 04 Sep 2012,
Published on 27 Jul 2010

The FindControl function with ASP.Net GridView control 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. The VB.Net 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 the checkbox control placed inside the ASP.Net GridView. Also you can access the properties of a 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" 
            HeaderStyle-HorizontalAlign="Left">
            <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" BackColor="#f0f0f0" />
</asp:GridView>

Using VB.Net FindControl Function

Dim chk As CheckBox

For Each rowItem As GridViewRow In GridView1.Rows
    chk = CType(rowItem.Cells(0).FindControl("chk1"), CheckBox)
    chk.Checked = CType(sender, CheckBox).Checked
Next

Above VB.Net code shows the use of FindControl function to access the Checked property of checkbox controls placed inside the GridView control. VB.Net for each 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 type ID parameter of FindControl function to access the checkbox control by ID.

Output:

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

VB.Net GridView FindControl Checkbox

Continue to next tutorial: Getting GridView CheckBox Value using VB.Net to learn how to get the value of checkbox control placed inside the ItemTemplate of the GridView control using FindControl method in VB.Net

0 Responses to "GridView FindControl Checkbox using VB.Net"
Leave a Comment
* required
* required
* will not be published
* optional
* hint: http://www.example.com
  • Subscribe via Email