0

I use a Checkbox in a Repeater, how can I know which Checkbox have changed in OnCheckedChanged? I have tried to set id then checkbox is binding data, but it will not work. Hope someone can help me

Thanks /Mats

2 Answers 2

2

Check the sender(Event Target) parameter

protected void Chb_Changed(object sender, EventArgs e)
{
    if (sender != null)
    {
      CheckBox cb=(CheckBox)sender;

      string clickedCheckBoxID=cb.ID;
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

Try following . Please note that we can also bind some primary column let's say "ID" column in some hidden field then get in code behind.

ASPX Side

<asp:Repeater ID="Repeater1" runat="server" DataSourceID="sqldtasource" >
       <ItemTemplate>
       <asp:CheckBox ID="chk" runat="server" AutoPostBack="true" Text='<%#Bind("Name")%>' OnCheckedChanged="Chb_Changed"/>
        <asp:HiddenField ID="hdn_ID" runat="server" Value='<%# DataBinder.Eval(Container.DataItem, "ID")  %>'/>
       </ItemTemplate>
 </asp:Repeater>

Code Behind :

protected void Chb_Changed(object sender, EventArgs e)
    {
        if (sender != null)
        {
            try
            {
                 var hdnID = (HiddenField)checkBox.NamingContainer       .FindControl("hf_ID");
                if(hdnID != null)
                 {
                   string primaryFieldValue = hdnID.Value;
                 }

                if (((CheckBox)sender).Checked)
                {
                    Response.Write(((CheckBox)sender).Text + " is checked");
                }
            }
            catch { 

                  }
        }
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.