Last post Sep 10, 2010 07:53 AM by chandrashekar
Sep 10, 2010 04:32 AM|CleanCodeNZ|LINK
We know that checkbox in a repeater control item template does not fire ItemCommand event, we can not use command argument or command name, How then can we handle check box click events knowing which row the checkbox on has triggered it?
Mainly I can find two credible solutions on the internet.
One solution <a href="http://www.frankwisniewski.net/how-to-make-a-checkbox-fire-the-itemcommand-event-of-a-repeater.aspx">provided here</a> is to extend checkbox control by adding ItemCommand support . Another solution is to attach event handling on repeater's
ItemCreate. Either soliton appears satisfactory to me.
The first one understands the problem very well, but it sounds bit heavyhanded to solve this problem by creating a custom control.
The second one is more traditional way of ASP.Net, but it has to loop through controls to find the right one to attach a handler, another issue about this solution is it did not show you how to pass the row parameter to the event handler.
Here I would like throw in my 2 cents on this little subject. Use a standard check box in your item template instead of a custom control, attach the event handler in declarative markup instead of looping though container in the code behind, store the identification
of row in a dummy attribute that can be retrieved later as a command argument, in the case of my example it is CssClass.
AutoPostBack = "True"
CssClass = '<%#DataBinder.Eval(Container.DataItem, "Id")%>'
Checked='<%# DataBinder.Eval(Container.DataItem, "MyBoolField")%>'
protected void MyCheckBox_CheckedChanged(object sender, EventArgs e)
if (sender != null)
CheckBox _typedSender = (CheckBox)sender;
int selectedid = int.Parse(_typedSender.CssClass);
Sep 10, 2010 07:53 AM|chandrashekar|LINK
In the repeater bind the Id value to a hidden field.
CheckBox chkBox = sender as CheckBox;
RepeaterItem item = chkBox.NamingContainer as RepeaterItem;
if(item == null) return;
HiddenField selectedValue = item.FindControl("HiddenField") as HiddenField;
int selected = (selectedValue != null && !string.IsNullOrEmpty(selectedValue.Value)) ? Convert.ToInt32(selectedValue.Value) : -1;