Dynamic Checkboxe Problem in ASP .NET

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!
 
Joined
May 16, 2006
Messages
27
Reaction score
0
private bool isButtonClick
{
get
{
if (ViewState["isButtonClick"] != null)
return Convert.ToBoolean(ViewState["isButtonClick"]);
return false;
}
set
{
ViewState["isButtonClick"] = value;
}
}
private void LoadCheckBox()
{
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);
}
protected void Page_Load(object sender, EventArgs e)
{
if (isButtonClick)
{
LoadCheckBox();
}
}
protected void Button1_Click(object sender, EventArgs e)
{
isButtonClick = true;
LoadCheckBox();
}

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";
}

}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,990
Messages
2,570,211
Members
46,796
Latest member
SteveBreed

Latest Threads

Top