Click here to Skip to main content
15,909,437 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I'm writing my first web application using C# ASP.NET. I have a socket client running in the CodeBehind that connects to an application running a socket server. I have controls in the ASP page that I want to update whenever the server notifies me of a state change. So for instance, if I have a Label contained in an UpdatePanel, I want to change the text if the server tells me it has changed. I have a callback function that gets called whenever the server sends me data that looks like this:

private void DataReceived(string data)
{
labelStatus.Text = ParseData(data);
}

ParseData just gets the desired text out of the data. However, the Label does not get updated because there is no PostBack.

What I have tried:

If I store the data in a static variable and run a timer to continually check to see if there is any unhandled data available, it will work because the timer triggers a PostBack event. Although it works, it is undesirable because the page is constantly refreshing even if nothing is changing.

Is there any way to update the label without using a timer?
Posted

1 solution

You can do a partial page postback or you can use Javascript to post data to the server behind the scene.
Refer MSDN for information on using AJAX and UpdatePanels
Partial-Page Rendering Overview[^]
To use Javascript you would need a Javascript timer - refer W3 Schools for Javascript timer JavaScript Timing Events[^]
And information on creating a XMLHTTP Post or Get
AJAX Send an XMLHttpRequest To a Server[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900