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")%>'
    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?

This entry was posted on Thursday, September 9th, 2010 at 2:02 am and is filed under ASP.NET. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

6 Responses to “Checkbox in Repeater Event Handling With Argument”

  1. MARYA GHADDU says:

    second post

  2. Daniello says:

    It Looks great. But why i get error like :
    “DataBinding: ‘System.Data.DataRowView’ does not contain a property with the name ‘Id’.” ?

Leave a Reply