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

Timer working differently inside a repeater in asp.NET 4?

$
0
0

Hi all,

I created a page with an UpdatePanel inside a repeater, with a Timer to cause a postback.
My problem is that all the postbacks seem to happen simultaneously, and each postback seems to happen several times.

Instead, if I create a page with several UpdatePanel and Timer controls but no repeater, the postbacks are fired one after another, and only once, which is what I would like to have.

Here is a small example to reproduce this problem: 

<asp:ScriptManager ID="ScriptManager1" runat="server" /><asp:Repeater ID="Repeater1" runat="server"><ItemTemplate><asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional" ChildrenAsTriggers="true"><ContentTemplate><asp:Timer ID="Timer1" runat="server" Enabled="true" OnTick="Timer1_Tick" Interval="500" /><asp:Label ID="Label1" runat="server" /></ContentTemplate></asp:UpdatePanel></ItemTemplate></asp:Repeater>

Codebehind:

protected void Page_Load(object sender, EventArgs e) {
    if (!IsPostBack) {
        Repeater1.DataSource = new int[5];
        Repeater1.DataBind();
    }
}

static int count = 0;

protected void Timer1_Tick(object sender, EventArgs e) {
    Timer timer = (Timer)sender;
    timer.Enabled = false;
    // System.Threading.Thread.Sleep(2000);        
    var l = (Label)timer.NamingContainer.FindControl("Label1");
    l.Text = string.Format("[{0}] {1}", count, DateTime.Now.ToString("HH:mm:ss"));
    count++;
}

Also, note that if I uncomment the "Thread.Sleep" line, it just keeps sending postabacks to the page (as the timer refresh interval is lower than the event handler execution time).

Another difference is that, if I register begin/end request handlers, they don't get called (while they get called in a page with multiple controls but not using a repeater):

<script type="text/javascript">
  function BeginRequestHandler(sender, args) {
    alert('BeginRequestHandler');
  }
  function EndRequestHandler(sender, args) {
    alert('EndRequestHandler');
  }
  Sys.WebForms.PageRequestManager.getInstance().add_beginRequest(BeginRequestHandler);
  Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequestHandler);</script>

Why is the behavior different?

What should I do to have the Timer inside the repeater behave like a "normal" timer on a page?

Thanks,
Paolo

NOTE: I tried the same code in an asp.net 3.5 and in an asp.net 4 application, and the behavior is different! The asp.net 3.5 application is behaving as I would expect, i.e. every postback is fired once. In the asp.net 4 application, instead, each postback can fire several times.


Viewing all articles
Browse latest Browse all 5678

Trending Articles



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