0

I'm using a Repeater:

<form id="form1" runat="server">
    <div>
        <asp:Repeater ID="repeater" runat="server" OnItemDataBound="repeater_ItemDataBound">
            <ItemTemplate>
                <tr>
                    <td>
                        <asp:CheckBox ID="checkbox" runat="server" AutoPostBack="false" OnCheckedChanged="Check_Clicked" data-id='<%# DataBinder.Eval(Container.DataItem, "ProfileID") %>'
                            Text="I agree" />
                    </td>
                    <td>
                        <asp:Label ID="lblProfileDesc" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "ProfileDesc") %>'></asp:Label>
                    </td>
                </tr>
                <br />
            </ItemTemplate>
        </asp:Repeater>
        <asp:Button ID="btn" runat="server" Text="Click" onclick="btn_Click" />
    </div>
    </form>

I'm handling onCheckedChange however how can I find out which checkbox was clicked? Thanks in advance!

Chandu
  • 81,493
  • 19
  • 133
  • 134
Dragan
  • 182
  • 1
  • 1
  • 10

2 Answers2

4

You can try

CheckBox checkBox = (CheckBox)sender;
var id = checkBox.Attributes["data-id"];
Bala R
  • 107,317
  • 23
  • 199
  • 210
  • Who voted up this answer? He said that he's not posting back on the check event of the check box, but at the click of the button. In other words, in the click handler of the button, the sender is the button, not the check box. – Saeed Neamati Jul 04 '11 at 18:47
  • @Saeed when AutoPostBack is set to false, the OnCheckedChanged will not fire when the checkbox is checked/unchecked but will fire during the next postback(could be triggered by the button or could be something else) and when it does, the sender will still be a CheckBox. – Bala R Jul 04 '11 at 18:50
  • Really strange; Haven't encountered this during my ASP.NET experience. Thanks :) – Saeed Neamati Jul 04 '11 at 18:52
0

Maybe you should redesign your UI architecture, as it's not acceptable to cause a full post back only for a change of a CheckBox (which is a Boolean parameter). I think you can use ajax here:

 $(function(){
     $('input[type=checkbox]').click(function(){
          // Initializing an ajax call here, and updating DOM based on response.
     });
 });
Saeed Neamati
  • 35,341
  • 41
  • 136
  • 188
  • AutoPostBack property is set to false so I'm handling the logic in the button_click event :) – Dragan Jul 04 '11 at 18:30
  • Yeah, you're right, I didn't notice that :). But still ajax call for Boolean operations like liking or disliking, checking or unchecking, etc. is so common these days ;) – Saeed Neamati Jul 04 '11 at 18:38
  • I know there's no escaping AJAX nowadays, however I'm studying jQuery atm. After I'm done with that I'll jump right into AJAX. Thanks for the suggestion, appreciated! Cheers – Dragan Jul 04 '11 at 18:42