0

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??

3
  • I don't have the answer to your question but the following article gives a nice check list for dynamically created controls: singingeels.com/Articles/… Commented Mar 14, 2016 at 18:25
  • I cant find a solution from that post :(... Commented Mar 14, 2016 at 18:41
  • That article suggests (in the 7 point check list) to recreate the controls on each postback. I thought it would work for you, considering your comment about the Page_Load function. Commented Mar 14, 2016 at 18:47

2 Answers 2

1

You can add a PlaceHolder to your Panel:

<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:PlaceHolder ID="ph" runat="server" />
</asp:Panel>

and insert the button in it, making sure that it is recreated on each postback:

protected void Button1_Click(object sender, EventArgs e)
{
    CreateDynamicButton();
    ViewState.Add("Button2", "Yes");
}

protected void CreateDynamicButton()
{
    Button button2 = new Button();
    button2.ID = "button2";
    button2.Text = "Dynamic Button";
    button2.Click += new EventHandler(button2_Click);
    ph.Controls.Clear();
    ph.Controls.Add(button2);
}

protected override void CreateChildControls()
{
    base.CreateChildControls();
    if (ViewState("Button2") == "Yes")
    {
        CreateDynamicButton();
    }
}

void button2_Click(object sender, EventArgs e)
{
    Label1.Text = "working";
}
Sign up to request clarification or add additional context in comments.

Comments

0

Probably you can have both the buttons in your aspx page with onclick property event defined for each of the buttons. Just keep the button2 visibility as false initially and onclick of button1 you can make the visibility of button2 as true. It's not a generic answer. It is a quick turnaround to your question specifically

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.