Search code examples
asp.netupdatepanel

asp.net UpdatePanel does not update


I have an asp.net website which contains a Thread that fetches some data from a WCF service. That thread runs in an infinite loop waiting each run for 1 second. Now I would like to show the stuff it got from the WCF service in a label. I added that label to an UpdatePanel and invoked the .Update() method. I don't get any exceptions, however, the label does not update at all. Here is my code (simplified):

t = new Thread(new ThreadStart(() =>
{
   while (true)
   {
      Label1.Text = GetFromWCF() + " " + DateTime.Now.ToString();
      updatePanel.Update();
      Thread.Sleep(1000);
   }
}
));

t.IsBackground = true;
t.Start();

This code is in the OnInit Method of the page. The updatePanel looks like this:

<asp:ScriptManager runat="server" ID="scriptManager" EnablePartialRendering="true"/>
<asp:UpdatePanel runat="server" ID="updatePanel" UpdateMode="Conditional">
   <ContentTemplate>
      <asp:Label ID="Label1" runat="server" />
   </ContentTemplate>
</asp:UpdatePanel>

Am I missing anything? Maybe I should also inform you that I am very new to asp.net.


Solution

  • You are not fully understand the way that Ajax and UpdatePanel work.

    Actually the UpdatePanel, from the client side, ask from the server data by making a post request and then waits to get them, after is get the data is update the client. The browser must make the request to get data - the server can not send to the browser any data, with out first browser ask for them.

    The command updatePanel.Update(); have the meaning to notify the UpdatePanel that there is an update, on code behind after the post - and is not working like you think, is not send data to the UpdatePanel just because you call it.

    Even if you make a close loop like that after the post, the UpdatePanel is wait the connection to fully complete and close to display the data, so a loop in a thread like that can not make a connection to send data.

    To make your idea work, ether create a timer on client side that ask for the data every some time, ether thing the comet technique : Reverse AJAX with IIS/ASP.NET