1

I have some fairly simple code here, but I can't for the life of me figure out why it's not working.

I have a page called Update.aspx which contains the following HTML:

<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>

<div>
    Non Panel <%= Date.Now.ToLongTimeString%>
</div>
<asp:UpdatePanel ID="UpdatePanel1" runat="server" ChildrenAsTriggers="False" UpdateMode="Conditional">
    <ContentTemplate>
        <asp:Label ID="lbl" runat="server">Updates in 5</asp:Label>
    </ContentTemplate>
</asp:UpdatePanel>

The code behind looks like this:

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim t As New Timers.Timer
        t.Interval = 5000
        AddHandler t.Elapsed, AddressOf raiseupdate
        t.Start()
    End Sub

    Private Sub raiseupdate(ByVal sender As Object, ByVal e As System.EventArgs)
        sender.stop()
        lbl.Text = Date.Now.ToLongTimeString
        UpdatePanel1.Update()
    End Sub

This is what I'm expecting to happen: The page displays the words "Updates in 5" within the update panel. The timer elapses, calls the raiseupdate() method, and the update panel update() method is called which refreshes the content of the update panel.

What actually happens is this: The timer elapses, the update panel update() method line is reached, but the data never seems to make it back to the page. That is, the words "Updates in 5" are never replaced with the current time.

So I figure I've run into some kind of fundamental misunderstanding about what the update() method actually does, but I can't figure out where I've gone wrong. What can I do to make this work?

Andy F
  • 1,517
  • 2
  • 20
  • 35
  • Threading in ASP.NET(which is multi-threading inherently) is not what i call _fairly simple_ . Why don't you use the [`Ajax Timer control`](http://ajax.net-tutorials.com/controls/timer-control/) instead? Timer and threads: http://stackoverflow.com/a/1436331/284240 – Tim Schmelter Sep 24 '12 at 13:20
  • Because I'm exploring the possibility of extending this example so that I can make asynchronous requests and update the updatepanel(s) when they return. _Then_ it will get complicated. – Andy F Sep 24 '12 at 13:39

1 Answers1

2

Looks like the two Updates.. One from the Update Panel and One from Timer is messing your code up.

Instead you can use ajax Timer and Add this as an AsyncPostBack triggers.. This should do the work for you..

<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>

<div>
    Non Panel <%= Date.Now.ToLongTimeString%>
</div>
<asp:Timer ID="Timer1" OnTick="Timer1_Tick" runat="server" Interval="5000" />

<asp:UpdatePanel ID="UpdatePanel1" runat="server" ChildrenAsTriggers="False" UpdateMode="Conditional">
    <ContentTemplate>
        <asp:Label ID="lbl" runat="server">Updates in 5</asp:Label>
    </ContentTemplate>
   <Triggers>
     <asp:AsyncPostBackTrigger ControlID="Timer1" />
  </Triggers>
</asp:UpdatePanel>

Your VB code will look like this

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

End Sub

Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As System.EventArgs)
     lbl.Text = Date.Now.ToLongTimeString

End Sub

If this does not work you can manually call the Update() in the Timer_tick event

Sushanth --
  • 55,259
  • 9
  • 66
  • 105
  • Inspired! This answer took me in a totally different direction but ultimately I think I can achieve what I want to using your method. – Andy F Sep 25 '12 at 10:31