Click here to Skip to main content
15,867,308 members
Articles / Web Development / ASP.NET

ASP.NET CheckBox Command in DataView or Repeater

Rate me:
Please Sign up or sign in to vote.
4.95/5 (25 votes)
9 Mar 2007CPOL2 min read 141.1K   1.4K   48   19
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)


Written By
Web Developer
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Questionthanks.... Pin
Member 1311453212-Oct-17 0:41
Member 1311453212-Oct-17 0:41 
QuestionTahnks.... Pin
m-e-h-d-h-i12-Mar-15 10:50
m-e-h-d-h-i12-Mar-15 10:50 
QuestionThanks Pin
Attiq-ul Islam9-Dec-14 23:56
Attiq-ul Islam9-Dec-14 23:56 
QuestionTips and immediate assistance regarding orders checkBox in asp.net .NET C # Pin
Member 1115563931-Oct-14 7:57
Member 1115563931-Oct-14 7:57 
QuestionCan You Explain Just More Option? Pin
irfanansari4-Apr-14 12:59
irfanansari4-Apr-14 12:59 
QuestionThank You! Pin
laynusfloyd17-Aug-12 7:30
laynusfloyd17-Aug-12 7:30 
GeneralDon't need to use CSS Pin
Member 452583531-Mar-09 5:08
Member 452583531-Mar-09 5:08 
GeneralRe: Don't need to use CSS Pin
Ruslan Sudentas31-Mar-09 5:28
Ruslan Sudentas31-Mar-09 5:28 
GeneralThis tip is very helpful Pin
uowzd0128-Oct-08 15:28
uowzd0128-Oct-08 15:28 
GeneralThanks a lot! Pin
Tatianared25-Jul-08 12:28
Tatianared25-Jul-08 12:28 
GeneralRe: Thanks a lot! Pin
Ruslan Sudentas28-Jul-08 1:25
Ruslan Sudentas28-Jul-08 1:25 
GeneralThank You Pin
JimmyRopes5-May-08 9:34
professionalJimmyRopes5-May-08 9:34 
Generalthanks Pin
the Asocial Ape22-Apr-08 5:03
the Asocial Ape22-Apr-08 5:03 
GeneralThanks bigtime Pin
Bryan Sumter10-Sep-07 4:13
Bryan Sumter10-Sep-07 4:13 
QuestionHow to update? Pin
wazodnuit2-Sep-07 7:54
wazodnuit2-Sep-07 7:54 
AnswerRe: How to update? Pin
Ruslan Sudentas4-Sep-07 4:35
Ruslan Sudentas4-Sep-07 4:35 
GeneralDropDownList example Pin
dx624-May-07 6:17
dx624-May-07 6:17 
GeneralThank You Pin
tjmonk158-May-07 10:55
tjmonk158-May-07 10:55 
GeneralThanks! Pin
ememar4719-Apr-07 10:00
ememar4719-Apr-07 10: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.