简体   繁体   中英

Is it possible to add an EventHandler to a control added at runtime after Page_Load?

based on the code below, it does not seem to be working. I can add a control dynamically to the page after Page_Load, but I cannot wire up an EventHandler to it.

Here is my code-behind:

public partial class AddingControlsToPanelAtRuntime : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        DrawPage();
    }

    private void DrawPage()
    {
        Label label;

        label = new Label();
        label.ID = "Label1";
        label.Text = "Label 1";
        Panel1.Controls.Add(label);
        Panel1.Controls.Add(new LiteralControl("<br />"));

        label = new Label();
        label.ID = "Label2";
        label.Text = "Label 2";
        Panel1.Controls.Add(label);
        Panel1.Controls.Add(new LiteralControl("<br />"));

        LinkButton linkButton1 = new LinkButton();
        linkButton1.ID = "linkButton1";
        linkButton1.Text = "linkButton 1";
        linkButton1.Click += new EventHandler(LinkButton_Click);
        Panel1.Controls.Add(linkButton1);
        Panel1.Controls.Add(new LiteralControl("<br />"));

        LinkButton linkButton2 = new LinkButton();
        linkButton2.ID = "linkButton2";
        linkButton2.Text = "linkButton 2";
        linkButton2.Click += new EventHandler(LinkButton_Click);
        Panel1.Controls.Add(linkButton2);

    }

    protected void LinkButton_Click(object sender, EventArgs e)
    {
        Panel1.Controls.Add(new LiteralControl("<p>"));
        LinkButton linkButton4 = new LinkButton();
        linkButton4.ID = "linkButton4";
        linkButton4.Text = "linkButton 4";
        linkButton4.Click += new EventHandler(LinkButton_Click);
        Panel1.Controls.Add(linkButton4);
    }

}

So, in DrawPage(), which is called from Page_Load, all the controls are added to the Panel just fine, and the EventHandlers wire up perfectly.

When I click on the LinkButton1 or LinkButton2, they correctly call LinkButton_Click(). In LinkButton_Click() I add another control at runtime and wire up and EventHandler, but when I go to click on that link button, it only posts back to Page_Load and does NOT call LinkButton_Click().

So, is what I'm trying to do here just not possible based on the ASP.NET Pipeline?

What you want to do is possible. Really being aware of page lifecycle will help. http://msdn.microsoft.com/en-us/library/ms178472.aspx

Here's whats currently happening on your page.

Page_Load
     Create Controls (label 1/2, linkbutton 1/2)

-> Browser renders label 1/2, linkbutton 1/2
-> User Clicks link button 1 or 2

Page_Load
     Create Controls (label 1/2, linkbutton 1/2)
Control events fire
     LinkButton_Click fires
          Adds another linkbutton (linkbutton 4)

-> Browser renders label 1/2, linkbutton 1/2 AND 4
-> User Clicks link button 4

Page_Load
     Create Controls (label 1/2, linkbutton 1/2)
Control events fire
     (There is no link button 4 so nothing happens)

-> Browser renders label 1/2, linkbutton 1/2 (4 is missing)

The core piece you are missing is re-adding dynamically created controls. Every control needs to be readded every post back (and in the same order if you care about viewstate). You can keep track of what needs to be readded via viewstate or any other postback persistant mechanism.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM