This is my .aspx page
<asp:Panel ID="Panel1" runat="server">
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
<asp:Label ID="Label1" runat="server"></asp:Label>
</asp:Panel>
This is my aspx.cs page
public partial class Test : System.Web.UI.Page
{
protected void Button1_Click(object sender, EventArgs e)
{
Button button2 = new Button();
button2.Text = "Dynamic Button";
button2.Click += new EventHandler(button2_Click);
Panel1.Controls.Add(button2);
}
void button2_Click(object sender, EventArgs e)
{
Label1.Text = "working";
}
protected void Page_Load(object sender, EventArgs e)
{
}
}
"button2"'s click even does not work. I also tried:
button2.Click += button2_Click;
But that does not seem to work either. If I place the code in the "Page_Load" function, it works. But I want the dynamic button to be added to the page on clicking the first button (i.e. Button1).
How do I make this work??