Quantcast
Channel: ASP.NET AJAX + Ajax Control Toolkit (ACT)
Viewing all articles
Browse latest Browse all 5678

Timer, UpdatePanel and dynamically created buttons

$
0
0

Hello all.

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.

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. Nothing I write in the btn1_click actually occurs.

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>

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



    }




Viewing all articles
Browse latest Browse all 5678

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>