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.
[aspx]
<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">
  <ItemTemplate>
    <div>
      <asp:CheckBox ID="CategoryID" runat="server" Text='<%# Eval("CategoryID") %>' />
    </div>
  </ItemTemplate>
</asp:Repeater>
                    
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="CategoryID"
     DataSourceID="AccessDataSource1">
  <Columns>
    <asp:TemplateField HeaderText="CategoryID" InsertVisible="False">
      <ItemTemplate>
         <asp:CheckBox ID="CategoryID" runat="server" Text='<%# Eval("CategoryID") %>' />
       </ItemTemplate>
    </asp:TemplateField>
    <asp:BoundField DataField="Description" HeaderText="Description" />
      <asp:BoundField DataField="CategoryName" HeaderText="CategoryName" />
  </Columns>
</asp:GridView>

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

<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]" />

GridView

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:

[C#]
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;
[VB]
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
Next
GrdLiteral.Text = Grd

Repeater

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:

[C#]
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;
[VB]
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

DataList

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:

[C#]
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;
[VB]
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
Next
DlLiteral.Text = Dl

Date Posted: Thursday, August 30, 2007 10:29 AM
Last Updated: Saturday, May 22, 2010 7:50 PM
Posted by: Mikesdotnetting
Total Views to date: 63556

6 Comments

Thursday, April 30, 2009 5:50 AM - Mohan

Hi
i am facing problem in retrieving the status of checkboxes
every time they showing as cheeck = false
here my code
.........
<Columns>
<asp:TemplateField>
<HeaderTemplate>
<asp:CheckBox ID="HeaderLevelCheckBox" runat="server" onclick="javascript:HeaderClick(this);" />
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" Text='<%# Eval("APPROVAL_STATUS") %>' />
</ItemTemplate>
</asp:TemplateField>
<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
Next

any help would highly be appreciated

Friday, September 11, 2009 8:09 AM - 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.

Wednesday, November 18, 2009 1:07 AM - 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!

-Tsu

Monday, March 8, 2010 10:15 AM - Mark

thanks! it's realy cool .
:)

Wednesday, February 16, 2011 3:51 AM - somasekhar akiri

nice article

Friday, January 11, 2013 7:41 AM - 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 some may not be published. The kind of things that will ensure your comment is deleted without ever seeing the light of day are as follows:

  • Not relevant to the article
  • Gratuitous links to your own site or product
  • Anything abusive or libellous
  • Spam
  • Anything in a language I don't understand including gibberish.

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

Gautam 11/20/2014 8:01 AM
In response to I'm Writing A Book On WebMatrix
Hello Mike, I read your book, loved it! However, I have a few request/suggestions: 1) an example...

Bret Dev 11/19/2014 8:39 PM
In response to The Difference Between @Helpers and @Functions In WebMatrix
Excellent post! One concern - where can you place global @Functions code within an MVC project to Is...

Rob Farquharson 11/19/2014 4:28 PM
In response to iTextSharp - Links and Bookmarks
How can I place text at an absolute position on the page? Also, how can I rotate text?...

Andy 11/17/2014 8:08 PM
In response to MVC 5 with EF 6 in Visual Basic - Sorting, Filtering and Paging
Hello I'm testing your sorting instructions above. This is great and I was able to get it to work...

Gautam 11/17/2014 5:51 PM
In response to WebMatrix - Database Helpers for IN Clauses
Hi Mike, I am very new to programming: In the above example if I want to use a delete button the...

donramon 11/17/2014 3:22 PM
In response to Entity Framework 6 Recipe - Alphabetical Paging In ASP.NET MVC
Congratulations on your new website look and the excellent articles. Thank you!...

Gautam 11/17/2014 11:26 AM
In response to Looking At The WebMatrix WebGrid
Hi Mike, I add the jquery script at the end of my html file.. when ajax attribute is added to the be...

Chet Ripley 11/15/2014 6:57 PM
In response to Adding A New Field
It appears the command is case sensitive. I had the same issue as Cameron. When I changed the to it...

Alvin 11/14/2014 12:49 PM
In response to Razor Web Pages E-Commerce - Adding A Shopping Cart To The Bakery Template Site
Great article Mike! When do you plan to extend the bakery shopping cart beyond this point?...

Gautam 11/14/2014 10:16 AM
In response to Web Pages - Efficient Paging Without The WebGrid
to get the count can we use only the below sql, why to join category and author table var sql =...