Referencing CheckBoxes in GridView, Repeater and DataList controls

If you want to find which CheckBoxes were selected in a multiple record DataBound control, the way that you you do it depends on the type of control you use. These examples demonstrate a CheckBox control being added to a GridView, Repeater and a DataList. For simplicity, I have placed all three controls on the same page, and used the Access version of theNorthwind database. One AccessDataSource control is used to bind the results of "SELECT [CategoryID], [Description], [CategoryName] FROM [Categories]" to all three controls.

<asp:Literal ID="RptLiteral" runat="server" /><br />
<asp:Literal ID="GrdLiteral" runat="server" /><br />
<asp:Literal ID="DlLiteral" runat="server" /><br />

<asp:Repeater ID="Repeater1" runat="server" DataSourceID="AccessDataSource1">
      <asp:CheckBox ID="CategoryID" runat="server" Text='<%# Eval("CategoryID") %>' />
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="CategoryID"
    <asp:TemplateField HeaderText="CategoryID" InsertVisible="False">
         <asp:CheckBox ID="CategoryID" runat="server" Text='<%# Eval("CategoryID") %>' />
    <asp:BoundField DataField="Description" HeaderText="Description" />
      <asp:BoundField DataField="CategoryName" HeaderText="CategoryName" />

<asp:DataList ID="DataList1" runat="server" DataKeyField="CategoryID" 
       <asp:CheckBox ID="CategoryID" runat="server" Text='<%# Eval("CategoryID") %>' /><br />
       <asp:Label ID="DescriptionLabel" runat="server" Text='<%# Eval("Description") %>' />
	   <br />
       <asp:Label ID="CategoryNameLabel" runat="server" Text='<%# Eval("CategoryName") %>' />
	   <br /><br />

<asp:Button ID="Button1" runat="server" Text="Button" />
<asp:AccessDataSource ID="AccessDataSource1" runat="server" DataFile="~/App_Data/Northwind.mdb"
  SelectCommand="SELECT [CategoryID], [Description], [CategoryName] FROM [Categories]" />


The GridView contains a collection of GridViewRow objects. Once you reference the collection, and iterate through it, you can use the FindControl method of the GridViewRow to access controls:

string Grd = "GridView Items Checked:<br />";
foreach (GridViewRow gvr in GridView1.Rows)
CheckBox chk = (CheckBox)gvr.FindControl("CategoryID");
if (chk.Checked)
    Grd += (chk.Text + "<br />");
GrdLiteral.Text = Grd;
Dim Grd As String = "GridView Items Checked:<br />"
For Each gvr As GridViewRow In GridView1.Rows
  Dim chk As CheckBox = DirectCast(gvr.FindControl("CategoryID"), CheckBox)
  If chk.Checked Then
    Grd += (chk.Text + "<br />")
  End If
GrdLiteral.Text = Grd


The Repeater has an Items collection. In this example, the Count property of the Items collection is retrieved and used with a for.. next loop [C#] While... End While [VB] to iterate the collection. The FindControl method of each Item is used to reference the CheckBox again:

string Rpt = "Repeater Items Checked:<br />";
for (int i = 0; i < Repeater1.Items.Count; i++)
  CheckBox chk = (CheckBox)Repeater1.Items[i].FindControl("CategoryID");
  if (chk.Checked)
    Rpt+=(chk.Text + "<br />");
RptLiteral.Text = Rpt;
Dim Rpt As String = "Repeater Items Checked:<br />"
Dim i As Integer = 0
While i < Repeater1.Items.Count
  Dim chk As CheckBox = DirectCast(Repeater1.Items(i).FindControl("CategoryID"), CheckBox)
  If chk.Checked Then
    Rpt += (chk.Text + "<br />")
  End If
  i += 1
End While
RptLiteral.Text = Rpt


The DataList also has an Items collection, but this time, foreach [C#] For Each [VB] is used to iterate the collection. for... next could just as easily be used as in the Repeater example:

string Dl = "Datalist Items Checked:<br />";
foreach (DataListItem dli in DataList1.Items)
  CheckBox chk = (CheckBox)dli.FindControl("CategoryID");
  if (chk.Checked)
    Dl += (chk.Text + "<br />");
DlLiteral.Text = Dl;
Dim Dl As String = "Datalist Items Checked:<br />"
For Each dli As DataListItem In DataList1.Items
  Dim chk As CheckBox = DirectCast(dli.FindControl("CategoryID"), CheckBox)
  If chk.Checked Then
    Dl += (chk.Text + "<br />")
  End If
DlLiteral.Text = Dl

Date Posted:
Last Updated:
Posted by:
Total Views to date: 68646


- Mohan

i am facing problem in retrieving the status of checkboxes
every time they showing as cheeck = false
here my code
<asp:CheckBox ID="HeaderLevelCheckBox" runat="server" onclick="javascript:HeaderClick(this);" />
<asp:CheckBox ID="CheckBox1" runat="server" Text='<%# Eval("APPROVAL_STATUS") %>' />
<asp:BoundField DataField="EMP_ID" HeaderText="EmpID" ReadOnly="True" Visible="False" />
<asp:BoundField DataField="EMP_NAME" HeaderText="Employee Name" ReadOnly="True" />

For Each gvRow As GridViewRow In GV.Rows
If gvRow.RowType = DataControlRowType.DataRow Then
Dim chkItem As CheckBox = DirectCast(gvRow.FindControl("CheckBox1"), CheckBox)
If chkItem.Checked = True Then
' do something
End If
End If

any help would highly be appreciated

- Ram

Greate post..
FindControl method not working for html controls(in my case it is, select control) in Datalist. I have placed Htmlselect control in one colunm but i unable to find it in foreach loop datalistitems.

- Tsubasa

This is an excellent example that I am also using in one of my programs for selecting and inserting records. I have yet to develop the code for inserting after selecting, but that should not be to hard for me.

Excellent work!


- Mark

thanks! it's realy cool .

- somasekhar akiri

nice article

- mrityunjay

it's very nice and really helpful.
Add your comment

If you have any comments to make about this article, please use this form to do so. Make sure that your comment relates specifically to the article above. More general comments can be posted through the form on the Contact page.

Please note, all comments are moderated, and I end up deleting quite a lot. The kind of things that will ensure your comment is deleted without ever seeing the light of day are as follows:

  • Requests to fix your code (post a question to instead, please)
  • Gratuitous links to your own site or product
  • Anything abusive or libellous
  • Spam

I do not pass email addresses on to spammers, so a valid one will assist me in responding to you personally if required.

Recent Comments

Lorenzo 3/26/2015 8:21 AM
In response to iTextSharp - Introducing Tables
Hi Mike How can I add padding to all cells in the table? Kind Regards Lorenzo...

Satyabrata Mohapatra 3/25/2015 8:11 AM
In response to How To Send Email In ASP.NET MVC
Great article. Simple and up to the point....

Afzaal Ahmad Zeeshan 3/24/2015 8:17 PM
In response to How To Send Email In ASP.NET MVC
A great way to teach the MVC framework for sending the emails... Also, what I found helpful was the...

Jim H 3/24/2015 2:32 PM
In response to Migrating From Razor Web Pages To ASP.NET MVC 5 - Model Binding And Forms
Thank you. This helps....

wazz 3/22/2015 5:48 AM
In response to Posting Data With jQuery AJAX In ASP.NET Razor Web Pages
great info!!...

rael 3/21/2015 8:53 PM
In response to Getting the identity of the most recently added record
I spent hours trying to figure how to achieve this in C#. This article helped me. Thanks a lot...

Stephen 3/21/2015 8:48 PM
In response to Ajax with Classic ASP using jQuery
This was very helpful, thanks:)...

patrick voes 3/19/2015 10:19 AM
In response to iTextSharp - Introducing Tables
Thank you! very helpfull....

Bigmachini 3/19/2015 6:13 AM
In response to ASP.NET MVC DropDownLists - Multiple Selection and Enum Support
This just made my day, afternoon, evening, night... was looking for a way of doing this without to a...

Bobbyg 3/19/2015 4:14 AM
In response to HTML Helpers For Forms In Razor Web Pages
Nice article. There are bugs in MVC 5 with dropdowns retaining values from other screens and them. I...