2

I would like to know why the event is not firing & how to find which checkbox control fired the event.

chkList1 = new CheckBox();
                            chkList1.Text = row["subj_nme"].ToString();
                            chkList1.ID = row["subjid"].ToString();
                            chkList1.Checked = true;
                            chkList1.Font.Name = "Verdana";
                            chkList1.Font.Size = 12;
                            chkList1.AutoPostBack = true;
                            chkList1.CheckedChanged += new EventHandler(CheckBox_CheckedChanged);
                            Panel1.Controls.Add(chkList1);

protected void CheckBox_CheckedChanged(object sender, EventArgs e)
            {
                Label1.Text = "Called";
            }
3
  • 1
    Where do you create the Checkbox dynamically? Do you recreate it also on postbacks in page_load at the latest with the same ID as before? Commented May 6, 2012 at 19:04
  • The object sender is the check box that triggered the event when you clicked on it. Just cast it to CheckBox. Commented May 6, 2012 at 21:41
  • i m creating Checkbox on page_load. Commented May 7, 2012 at 4:59

1 Answer 1

2

If the events aren't firing, it's likely for one of two reasons:

  1. The controls are recreated too late in the page lifecycle. Try creating the controls during OnInit.
  2. Validation is preventing the postback. To work around this you can set CausesValidation to false on all of the CheckBox controls.

You can find out which control triggered the event using the sender argument.

protected void CheckBox_CheckChanged(object sender, EventArgs e)
{
    //write the client id of the control that triggered the event
    Response.Write(((CheckBox)sender).ClientID);
}
Sign up to request clarification or add additional context in comments.

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.