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}.

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

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:

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

However, I am using an updatepanel where i putted all of my check boxes. As a result, it is not posting back, and i also don't want post back. Therefore, the event , CheckedChanged, is not firing.So, without post back, how i can fire the CheckedChanged event to send checked or unchecked status to the code behind?

How MAy I Use Check Box Selection Multiple Row With Many Checked Boxes Select and Then just updated it by update button command the database will assing a new value to the database field like1 job1 datedpost status

after check box chedk staus shows a new value like this1 job1 datedpost Public

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:

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.

Parameter.DefaultValue specifies a default value in case the parameter is uninitialized when the Evaluate method is called. It is a wrong property to call in this case. Parameter objects are evaluated at run time, you can not explicitly set it at design time.I do not use DataSourceControl in this sample precisely because of this reason. What I do in this case, I explicitly call a DAL class method to update a particular record. In other words this line: