I'm fairly new to asp.net CompositeControls... I have a task which requires me to add a 1 button on load, then when the user clicks it, another button gets added, and when the second button is clicked a label is shown.
The problem is after clicking the 2nd button and the page finishes postback, the whole page returns to its initial state and no label is shown. So the 1st Button click fires but the 2nd event doesn't. I even created a breakpoint to check, it doesn't access "btn_submit2_Click"
test1Composite.cs:
using System;
using System.ComponentModel;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace test1File
{
[DefaultProperty("Text")]
[ToolboxData("<{0}:test1 runat=server></{0}:test1>")]
public class test1 : CompositeControl
{
protected Button btn_submit1;
protected Button btn_submit2;
[Bindable(true)]
[Category("Appearance")]
[DefaultValue("")]
[Localizable(true)]
protected override void CreateChildControls()
{
btn_submit1 = new Button();
btn_submit1.Text = "Click me!";
btn_submit1.Click +=new EventHandler(btn_submit1_Click);
this.Controls.Add(btn_submit1);
this.ChildControlsCreated = true;
}
protected void btn_submit1_Click(object sender, EventArgs e)
{
btn_submit2 = new Button();
btn_submit2.Text = "Click me!";
btn_submit2.Click += new EventHandler(btn_submit2_Click);
this.Controls.Add(btn_submit2);
}
protected void btn_submit2_Click(object sender, EventArgs e)
{
Label lbl_done = new Label();
lbl_done.Text = "Thank you :)";
this.Controls.Add(lbl_done);
}
}
}
Default.cs:
using System;
namespace test1File
{
public partial class Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
test1 pl = new test1();
form1.Controls.Add(pl);
}
}
}
I've searched online and found that "maybe" i need to override OnInit but if i put CreateChildControls() it doesn't help... maybe i'm doing something wrong here.. any help would be appreciated, thank you.