简体   繁体   中英

How to add a control with an event handler dynamically to an update panel and have the event handler post back via AJAX

Here is my attempt, for some reason it is not working. I am at a loss, tried a ton of stuff. Perhaps this is not even possible?

aspx:

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    <asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
    <asp:UpdatePanel ID="UpdatePanel1" runat="server">
        <ContentTemplate>
            <asp:PlaceHolder ID="PlaceHolder1" runat="server">
                <asp:Panel ID="Panel1" runat="server">
                    <asp:Panel ID="Panel2" runat="server">

                    </asp:Panel>
                </asp:Panel>
            </asp:PlaceHolder>
        </ContentTemplate>
    </asp:UpdatePanel>
</asp:Content>

code behind:

protected void Page_Load(object sender, EventArgs e)
        {
            Button aa = new Button();
            UpdatePanel1.ContentTemplateContainer.Controls.Add(aa);
            Panel2.Controls.Add(aa);
            aa.Click += new System.EventHandler(this.Button1_Click);
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            Button bb = new Button();
            UpdatePanel1.ContentTemplateContainer.Controls.Add(bb);
            Panel2.Controls.Add(bb);
            bb.Click += new System.EventHandler(this.Button2_Click);
            int i = 0;
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            int i = 0;
        }

The first button gets added with no problems. When I click it, it does an AJAX postback to Button1_Click and adds button number 2. However, when I click button number 2, it does a regular postback, disappears and the event handler never fires.

All help is appreciated, thanks in advance.

In asp.net is not possible to able to automatically maintain the state of dynamically generated controls.

So you need some mechanism to maintain the state of dynamically generated control. Like below, it will helpful to you.

protected void Page_Load(object sender, EventArgs e)
    {
        Button aa = new Button();
        UpdatePanel1.ContentTemplateContainer.Controls.Add(aa);
        Panel2.Controls.Add(aa);
        aa.Click += new System.EventHandler(this.Button1_Click);
        if(Convert.ToString(ViewState["AddTwoButton"]) == "1")
            CreateSecoundButton();
    }
    private void CreateSecoundButton()
    {
        Button bb = new Button();
        UpdatePanel1.ContentTemplateContainer.Controls.Add(bb);
        Panel2.Controls.Add(bb);
        bb.Click += new System.EventHandler(this.Button2_Click);
        int i = 0;
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        CreateSecoundButton();
        ViewState["AddTwoButton"] = "1";
    }

    protected void Button2_Click(object sender, EventArgs e)
    {
        int i = 0;
    }

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