Click here to Skip to main content
Click here to Skip to main content

ASP.NET CheckBox Command in DataView or Repeater

, 9 Mar 2007 CPOL
Rate this:
Please Sign up or sign in to vote.
Describes a very simple way on how to use a CheckBox in a GridView or Repeater in a manner similar to a Button.

Screenshot - main.gif

Introduction

This article describes a very simple way on how to use a CheckBox in a GridView or Repeater in a manner similar to a Button. This addresses the absence of the "CommandName" property in a CheckBox (CheckBoxField).

Use Cases

A user goes through a list of items and needs a quick way to flag some of the items. For example, to indicate what items are completed (or verified). And maybe, update a DateCompleted field on completion.

Unchecking in this case would change the status back to "Uncompleted" and maybe clear the DateCompleted field.

Limitations of ASP.NET

The standard way of achieving similar results would be to use a Button (or LinkButton) with the CommandName specified. The OnRowCommand event handler for a GridView (OnItemCommand for a Repeater) would handle the Click on such a button. Then, you could easily access the record ID through e.CommandArgument in these handlers.

In many of the cases like the one above, it would be preferable to use a CheckBox instead – it is more compact and clearly indicates the current state. But since there is no "CommandName" property for a CheckBox and the CheckedChanged event is not handled in OnRowCommand (OnItemCommand for a Repeater), how can we get a record ID for the checkbox row, so we could properly handle the event?

Solution

It can't get any simpler than that: just assign a record ID as a Text property for the CheckBox and then hide the text through CSS.

Example

The example consists of a single ASPX file that has both a GridView and a Repeater that are bound to a DataTable with columns {ItemID int, ItemDescription string, Flag bool}.

Screenshot - Form.gif

Both the GridView and the Repeater have a TemplateField containing a CheckBox.

Screenshot - CheckBox.gif

The whole trick is in Text='<%#Eval("ItemID")%>'. Note that ItemID is hidden because the Text property for a CheckBox is rendered as <label> and we hide it with this style:

Screenshot - css.gif

The event hander now can easily access the current record ID and the status of the CheckBox:

Screenshot - handler.gif

License

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

Share

About the Author

Ruslan Sudentas
Web Developer
United States United States
No Biography provided

Comments and Discussions

 
QuestionThanks [modified] PinmemberAttiq-ul-Islam10-Dec-14 0:56 
QuestionTips and immediate assistance regarding orders checkBox in asp.net .NET C # PinmemberMember 1115563931-Oct-14 8:57 
QuestionCan You Explain Just More Option? Pingroupirfanansari4-Apr-14 13:59 
QuestionThank You! Pinmemberlaynusfloyd17-Aug-12 8:30 
GeneralDon't need to use CSS PinmemberMember 452583531-Mar-09 6:08 
I had this issue just yesterday, and it caused me a huge headache. I wish I'd have found your article then, but I ended up piecing together nearly the same solution from postings on the ASP.NET forums. The only thing that was different was that I didn't use any CSS. It turns out that you can add in completely arbitrary attributes to web controls. My CheckBox control already has ID="myCheckBox" runat="server", etc., and I added in:
 
PimaryKey=<%# Eval("PrimaryKey")%>
 
In the code behind, after casting the sender object to the CheckBox control just as you did, I was able to grab the primary key value by grabbing the attribute like:
 
CheckBox chk = (CheckBox)sender;
int pk = Convert.ToInt32(chk.Attributes["PrimaryKey"]);

 
You can snag the row index (if you want) like this:
 
ListViewItem item = (ListViewItem)chk.NamingContainer;
int rowIndex = myListView.Items.IndexOf((ListViewDataItem)item);

 
And there it is. You only have to cast the item into a ListViewDataItem for the last bit of information. In truth, I feel somewhat aggravated about the "CommandName" property. If MicroSoft was going to expose the update event for the ListView, FormView, etc. as a CommandName, it might have been a good idea to add that to all the controls that could fire it off. If not, allow the ListViewEditEventArgs to be used in a custom method. If not even that, allow easy retrieval of the row index from EventArgs.
 
Thanks again for posting this all in one place.
GeneralRe: Don't need to use CSS PinmemberRuslan Sudentas31-Mar-09 6:28 
GeneralThis tip is very helpful Pinmemberuowzd0128-Oct-08 16:28 
GeneralThanks a lot! PinmemberTatianared25-Jul-08 13:28 
GeneralRe: Thanks a lot! PinmemberRuslan Sudentas28-Jul-08 2:25 
GeneralThank You Pinmember JimmyRopes 5-May-08 10:34 
Generalthanks Pinmemberthe Asocial Ape22-Apr-08 6:03 
GeneralThanks bigtime Pinmemberraiden8810-Sep-07 5:13 
QuestionHow to update? Pinmemberwazodnuit2-Sep-07 8:54 
AnswerRe: How to update? PinmemberRuslan Sudentas4-Sep-07 5:35 
GeneralDropDownList example Pinmemberdx624-May-07 7:17 
GeneralThank You Pinmembertjmonk158-May-07 11:55 
GeneralThanks! Pinmemberememar4719-Apr-07 11:00 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.141223.1 | Last Updated 10 Mar 2007
Article Copyright 2007 by Ruslan Sudentas
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid