简体   繁体   English

计时器,UpdatePanel和动态创建的按钮

[英]Timer, UpdatePanel and dynamically created buttons

I have an update panel with a div (with runat="server") so I dynamically create and add buttons to it on a timer tick event. 我有一个带有div(带有runat =“ server”)的更新面板,因此我在计时器滴答事件中动态创建按钮并为其添加按钮。

The problem is that when a button is click, the is a postback but the button is gone and the event is not raised. 问题是单击按钮时,该按钮是回发操作,但是按钮消失了,并且不会引发事件。

Here's my aspx page and my c# code behind. 这是我的aspx页面和后面的c#代码。 Nothing I write in the btn1_click actually occurs. 我在btn1_click中写的内容实际上没有发生。

There's a continue to my question but in order to keep it simple, I would like to understand the above first. 我的问题还有一个问题,但是为了简单起见,我想先了解一下上面的内容。

I really need your help and i appreciate it, ty. 我真的需要您的帮助,非常感谢。

<body>
    <form id="form1" runat="server">
    <asp:ScriptManager ID="ScriptManager1" runat="server">
    </asp:ScriptManager>
    <asp:Timer ID="Timer1" runat="server" Interval="2000" ontick="Timer1_Tick">
    </asp:Timer>
    <asp:UpdatePanel ID="UpdatePanel1" UpdateMode="Conditional" runat="server">
        <Triggers>
            <asp:AsyncPostBackTrigger ControlID="Timer1" EventName="Tick" />
        </Triggers>
        <ContentTemplate>
            <div id="div1" runat="server"></div>
        </ContentTemplate>
    </asp:UpdatePanel>
    <div id="div2" runat="server">div2</div>
    </form>
</body>

and this c# here. 和这个C#在这里。

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

            protected void Timer1_Tick(object sender, EventArgs e)
            {
                CreateControl();
            }

            void CreateControl()
            {
                Button btn1 = new Button();
                btn1.ID = "btn1";
                btn1.Text = "click me";
                btn1.Click += new EventHandler(btn1_Click);
                div1.Controls.Add(btn1);
            }

            void btn1_Click(object sender, EventArgs e)
            {
                div1.InnerHtml += "btn1 was clicked";
            }



        }

For dynamically added controls you must add them in every page load event. 对于动态添加的控件,必须在每个页面加载事件中添加它们。 Otherwise by the time it gets to the click event the are already gone. 否则,在到达点击事件时,它们已经消失了。 In this case I would add the button to a session collection of buttons every time you create one, then reload that entire collection on every postback, so it doesn't lose what you've already added. 在这种情况下,我会在每次创建按钮时将按钮添加到会话的按钮集合中,然后在每次回发时重新加载整个集合,因此它不会丢失您已经添加的内容。 Also, save an int in session that you increment so they dont all have the same ID. 另外,在您增加的会话中保存一个int值,这样它们就不会都具有相同的ID。 IDs should be unique ID应该是唯一的

something like: 就像是:

 protected void Page_Load(object sender, EventArgs e)
        {
          if(Page.IsPostBack)
           {
             CreateControls(YourCollectionInSession)
           }
        }
 protected void Timer1_Tick(object sender, EventArgs e)
        {
            CreateControl();
        }
 void CreateControl()
        {
            Button btn1 = new Button();
            btn1.ID = "btn" + yourSessionID;
            btn1.Text = "click me";
            btn1.Click += new EventHandler(btn1_Click);
            div1.Controls.Add(btn1);
            yourSessionID++;
            YourCollectionInSession.Add(btn1);
        }
 void CreateControl(List<Button> buttons)
        {
            foreach(Button btn in buttons)
            {
            div1.Controls.Add(btn); 
            }
        }

I've had to do the same thing before and this approach worked for me. 我之前必须做同样的事情,这种方法对我有用。 Just remember the ASPx Page Lifecycle . 只要记住ASPx页面生命周期 It will always hit the page_init and page_load before it handles any post back events, meaning your controls are long gone. 在处理任何回发事件之前,它将始终命中page_init和page_load,这意味着您的控件早已不复存在。

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

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