Reputation: 304
I am programming a Web Application with ASP.NET and C#.
When the page loads, it has to listen (IP connection) and when it gets a message, a label should be updated with that value (update the UI from the callback). Simplifying my code, I have this for the mainpage:
protected void Page_Load(object sender, EventArgs e)
{
Thread listeningThread = new Thread(new ThreadStart(StartListening));
listeningThread.Start();
}
This is the label in the aspx file:
<asp:Label ID="MyLabel" runat="server" EnableViewState="False"></asp:Label>
Then, this is my function for the callback:
private void StartListening()
{
Action<string> callbackRead = (string message) =>
{
myLabel.text = message;
};
}
The message arrives properly and if I put a breakpoint inside the callback I can see how it goes inside it. Despite of this, UI is not updated. Of course, if I write myLabel.text = "Whatever"; in the Page_Load part, it works fine.
I have tried with a grid and DataSource + DataBind() (I followed this tutorial) but I still have the same problem, it works fine if I do it in the Page_Load but not from the callback.
I have read a lot about this in another questions but non of the answers solves my problem (most are for WinForms, etc.)
I'm new in this field and I know I'm probably missing something. Could you help me, please? How is the right way to achieve what I want? Thanks in advance!
Upvotes: 0
Views: 278
Reputation: 4833
By setting myLabel.text = message you update label text on the server side while you [also] need to update text in client browser(s).
Typical approaches here are: ajax pulling (you send ajax requests each X seconds to server to check for updates) or websockets (your server can push updates directly to client browsers).
Upvotes: 2