I
Iderocks
Hi All,
I created a dynamic checkbox in ASP .Net inside a Button1_Click event method
(outside the page_load event) and performed the event handling method for the
CheckedChanged event and I check the checkbox at runtime nothing happens
(checkBoxObj.check is always false). Does anyone have information on how to
make this work? Note: I can get it to work inside Page_Load event. I need
help in getting it to work in different event method. See my below code
snippet:
protected void Button1_Click(object sender, EventArgs e)
{
// Create new CheckBox control.
CheckBox NewCheckBox = new CheckBox();
NewCheckBox.ID = "CheckBoxId";
NewCheckBox.Text = "Dynamic CheckBox";
NewCheckBox.AutoPostBack = true;
//// Register the event-handling method for the CheckedChanged event.
NewCheckBox.CheckedChanged += new EventHandler(this.Check_Change);
// Add the control to the Controls collection of the
// PlaceHolder control.
Place.Controls.Clear();
Place.Controls.Add(NewCheckBox);
}
void Check_Change(Object sender, EventArgs e)
{
// Retrieve the CheckBox control from the PlaceHolder control.
CheckBox check = (CheckBox)Place.FindControl("CheckBoxId");
// Display the appropriate message based on the state of the
// CheckBox control.
if (check.Checked)
{
TextBox1.Text = "I'm checked";
}
else
{
TextBox1.Text = "I'm not checked";
}
}
Thanks!
I created a dynamic checkbox in ASP .Net inside a Button1_Click event method
(outside the page_load event) and performed the event handling method for the
CheckedChanged event and I check the checkbox at runtime nothing happens
(checkBoxObj.check is always false). Does anyone have information on how to
make this work? Note: I can get it to work inside Page_Load event. I need
help in getting it to work in different event method. See my below code
snippet:
protected void Button1_Click(object sender, EventArgs e)
{
// Create new CheckBox control.
CheckBox NewCheckBox = new CheckBox();
NewCheckBox.ID = "CheckBoxId";
NewCheckBox.Text = "Dynamic CheckBox";
NewCheckBox.AutoPostBack = true;
//// Register the event-handling method for the CheckedChanged event.
NewCheckBox.CheckedChanged += new EventHandler(this.Check_Change);
// Add the control to the Controls collection of the
// PlaceHolder control.
Place.Controls.Clear();
Place.Controls.Add(NewCheckBox);
}
void Check_Change(Object sender, EventArgs e)
{
// Retrieve the CheckBox control from the PlaceHolder control.
CheckBox check = (CheckBox)Place.FindControl("CheckBoxId");
// Display the appropriate message based on the state of the
// CheckBox control.
if (check.Checked)
{
TextBox1.Text = "I'm checked";
}
else
{
TextBox1.Text = "I'm not checked";
}
}
Thanks!