Checkbox in Repeater Event Handling With Argument
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 provided here 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.
<asp:CheckBox ID="CheckBox1"
AutoPostBack = "True"
CssClass = '<%#DataBinder.Eval(Container.DataItem, "Id")%>'
oncheckedchanged="MyCheckBox_CheckedChanged"
Checked='<%# DataBinder.Eval(Container.DataItem, "MyBoolField")%>'
runat="server" />
Following is the event handler in code behind
protected void MyCheckBox_CheckedChanged(object sender, EventArgs e)
{
if (sender != null)
{
CheckBox _typedSender = (CheckBox)sender;
int selectedid = int.Parse(_typedSender.CssClass);
}
}
Is not it simpler?

first post
second post
It Looks great. But why i get error like :
“DataBinding: ‘System.Data.DataRowView’ does not contain a property with the name ‘Id’.” ?
i get it why: it must be something binded to repeater, now i dont know what is “MyBoolField”. Sorry for spam..
ok, this im my last msg here, thx and sorry, ispostback did the job and event works, even without your code,
thx anyway
“MyBoolField” is the field name that has a type of bool which you want to data bind with the checkbox.