Christian
Christian

Reputation: 4375

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.

Upvotes: 0

Views: 13304

Answers (3)

Pushpendra
Pushpendra

Reputation: 810

try using triggers.

<asp:UpdatePanel runat="server" ID="update" UpdateMode="Conditional">
     <ContentTemplate>
         <asp:Label runat="server" ID="uiTime" />
         <asp:Button runat="server" ID="uiInternalButton" Text="Click" />
     </ContentTemplate>
     <Triggers>
         <asp:AsyncPostBackTrigger ControlID="uiAsynch" EventName="click" />
         <asp:PostBackTrigger ControlID="uiInternalButton" />
     </Triggers>
</asp:UpdatePanel>
<asp:Button runat="server" ID="uiPostback" Text="Click" />

Upvotes: 2

Phill
Phill

Reputation: 1423

Bit late to the party, but you can work around this by implementing a Timer object within the UpdatePanel, and then using the timer "OnTick" event to update the Label. You can probably then rework your Thread function by outputting the required values into a global variable and/or with a static implementation.

<asp:UpdatePanel ID="MyPanel" UpdateMode="Conditional" runat="server">
    <ContentTemplate>
    <asp:Timer ID="MyTimer" OnTick="timer_tick" Interval="1000" runat="server" />
    <asp:Label ID="UpdateTextBox" Text="start" runat="server" />
    </ContentTemplate>
</asp:UpdatePanel>

protected void timer_tick(object sender, EventArgs e)
{
    UpdateTextBox.Text = Convert.ToString(myThreadClass.counter);
    MyPanel.Update();
}

Upvotes: 4

Aristos
Aristos

Reputation: 66641

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

Upvotes: 3

Related Questions