简体   繁体   English

如何将带有事件处理程序的控件动态添加到更新面板,以及如何通过AJAX将事件处理程序回发

[英]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: 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. 当我单击它时,它将对Button1_Click进行AJAX回发并添加按钮号2。但是,当我单击按钮2时,它将进行常规的回发,并且消失并且事件处理程序从不触发。

All help is appreciated, thanks in advance. 感谢所有的帮助,在此先感谢。

In asp.net is not possible to able to automatically maintain the state of dynamically generated controls. 在asp.net中,不可能自动维护动态生成的控件的状态。

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;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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