Click here to Skip to main content
13,354,733 members (60,572 online)
Click here to Skip to main content
Add your own
alternative version


19 bookmarked
Posted 8 Apr 2008

The RIGHT Way to Use Checkboxes in a .NET Repeater!

, 9 Feb 2009
Rate this:
Please Sign up or sign in to vote.
How to generate checkboxes corresponding to items in a repeater, and how to retrieve their state on PostBack


It's one year later. I've gained some architectural insight, and to those who've been critical of this approach, I see your point. That said, the code here does work properly -  it's just highly inelegant.


ASP.NET server controls are designed to allow for event-driven programming and stateful behavior in the inherently stateless environment of the web. This is a good thing as it frees the programmer from having to parse Forms and QueryStrings and instead allows the developer to address visual elements on the page (i.e. text fields, drop down lists, labels, etc.) the same way you would deal with elements in a desktop application. The result is a huge savings of time, a nearly complete separation of UI and code, and an application that is far easier to debug.

Unfortunately, many of these controls are buggy and/or have missing features. Most notably, the <asp:CheckBox> control.

A common scenario in web development involves a list of checkboxes next to a bunch of items in a form. After submission of the form, the checked items are dealt with in some way (i.e. multiple deletion of messages in Hotmail.) Due to limitations in the CheckBox server control and a lack of documentation surrounding the proper use of Repeaters, developers have historically dealt with this problem by using a client side CheckBox, JavaScript, and an old-school cross page form post.


The problem is fundamentally this: server side CheckBox controls lack a "Value" attribute. Therefore, it is difficult to use them with dynamically generated lists of data in a Repeater, as there is no straightforward way to determine which checkboxes represent which data item after the form is posted back.

There have been many solutions posted on the web which involve re-evaluating each data item in the repeater after PostBack using a looping approach. This method is error prone (such as with forward only readers) as well as being inefficient. The method I present here instead retrieves the data only once, stores it in a hidden server control paired with the checkbox, and allows for quick and easy retrieval on PostBack.

The Example

The following snippets show a typical example where a customer list is displayed, with a checkbox next to each customer. After the user clicks a button, messages are sent only to the customers who they have checked off.

The Front-End Code

<asp:Repeater id="customerList" runat="server">
   <%# Eval("name") %><br />  <!-- Their name -->
   <%# Eval("email") %><br /> <!-- Their email -->
   Send mailing list invite <asp:CheckBox id="selectUser" runat="server">
   <!-- Line below is key -->
   <asp:HiddenField ID="hiddenEmail" Value ='<%#Eval("email")%>'>

<asp:Button Text="Send Message" OnClick="DoSend" Runat="server" />

What we have done here is place an invisible server control, the asp:HiddenField, inside the Repeater template. Since this control DOES allow a value to be assigned to it, we can now determine which checkbox is linked to which row.

To retrieve the email addresses associated with the checked items after PostBack, we need only to use a simple loop, as follows:

The Code-Behind

public void DoSend(object sender, EventArgs e)
    foreach (RepeaterItem i in customerList.Items)
          //Retrieve the state of the CheckBox
          CheckBox cb = (CheckBox)i.FindControl("selectUser");
          if (cb.Checked)
               //Retrieve the value associated with that CheckBox
               HiddenField hiddenEmail = (HiddenField)i.FindControl("hiddenEmail");

               //Now we can use that value to do whatever we want

And that's all, folks! May the "I want to bang my head against the wall because I can't use a checkbox insider a repeater" phenomenon be but a distant memory.

Points of Interest

Microsoft has put a lot of work into its GridView control (for example, this particular problem is not an issue there) but really neglected its Repeater, which hasn't even changed significantly since ASP.NET 2.0.

Many coders choose the GridView over the Repeater partly for this reason. Repeaters are far more lightweight, and allow for full control over what is rendered. Hopefully this workaround will help to encourage its use.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Sam Rahimi
Team Leader World Golf Tour
Canada Canada
Sam Rahimi currently leads the web team at World Golf Tour ( and has an extensive background in ASP.NET, specializing in social networking and casual gaming.

Previously, he has worked for Roblox Corporation on their unique children's MMO as well as spending two years as team lead at, helping bring their social networking site into the modern era and doubling traffic along the way.

Sam started as a classic ASP engineer as a summer job 6 years ago to make some money to pay for tuition - to finish a degree in political science - and needless to say, never looked back. His experience has lead him to gain additional experience in the mobile space - J2ME, Android app developmentm, and SMS protocols.

And sure, the other engineers in SF may have an IPhone - Rahimi sticks with the EVO all the way!

You may also be interested in...


Comments and Discussions

GeneralMy vote of 5 Pin
vishwajitg22-Aug-12 5:52
membervishwajitg22-Aug-12 5:52 
QuestionGreat Post Pin
shawn meggoe4-May-12 8:10
membershawn meggoe4-May-12 8:10 
AnswerI found an easier solution Pin
freecellwizard18-Jun-09 5:35
memberfreecellwizard18-Jun-09 5:35 
GeneralRe: I found an easier solution Pin
freecellwizard18-Jun-09 5:43
memberfreecellwizard18-Jun-09 5:43 
QuestionInheritance? Why? Pin
otijim12-May-09 6:58
memberotijim12-May-09 6:58 
Generalgood 1 Pin
ashu2chandra17-Jun-08 22:17
memberashu2chandra17-Jun-08 22:17 
this one is quite good.


GeneralAnother option is... Pin
tkrafael_net9-Apr-08 6:19
membertkrafael_net9-Apr-08 6:19 
GeneralRe: Another option is... Pin
Sam Rahimi9-Apr-08 7:55
memberSam Rahimi9-Apr-08 7:55 
GeneralRe: Another option is... Pin
tkrafael_net9-Apr-08 9:07
membertkrafael_net9-Apr-08 9:07 
QuestionInheritance? Pin
Rogic9-Apr-08 1:35
memberRogic9-Apr-08 1:35 
AnswerRe: Inheritance? Pin
evolved9-Apr-08 3:24
memberevolved9-Apr-08 3:24 
GeneralRe: Inheritance? Pin
Sam Rahimi9-Apr-08 4:59
memberSam Rahimi9-Apr-08 4:59 
GeneralRe: Inheritance? Pin
evolved9-Apr-08 7:51
memberevolved9-Apr-08 7:51 
GeneralRe: Inheritance? Pin
Sam Rahimi9-Apr-08 8:10
memberSam Rahimi9-Apr-08 8:10 
GeneralRe: Inheritance? Pin
evolved9-Apr-08 8:22
memberevolved9-Apr-08 8:22 
RantRe: Inheritance? Pin
tkrafael_net9-Apr-08 9:15
membertkrafael_net9-Apr-08 9:15 
GeneralRe: Inheritance? Pin
Sam Rahimi9-Apr-08 12:18
memberSam Rahimi9-Apr-08 12:18 
GeneralRe: Inheritance? Pin
tkrafael_net9-Apr-08 15:34
membertkrafael_net9-Apr-08 15:34 
GeneralRe: Inheritance? Pin
evolved11-Apr-08 7:03
memberevolved11-Apr-08 7:03 
GeneralRe: Inheritance? Pin
Anthony Missico10-Nov-08 9:22
memberAnthony Missico10-Nov-08 9:22 
GeneralRepeater and late binding Pin
rippo8-Apr-08 20:33
memberrippo8-Apr-08 20:33 
GeneralRe: Repeater and late binding Pin
Sam Rahimi9-Apr-08 5:00
memberSam Rahimi9-Apr-08 5:00 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.180111.1 | Last Updated 9 Feb 2009
Article Copyright 2008 by Sam Rahimi
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid