更新asp控件而不回发事件 [英] Updating asp controls without postback event
问题描述
我正在使用C#ASP.NET编写我的第一个Web应用程序。我有一个在CodeBehind中运行的套接字客户端,它连接到运行套接字服务器的应用程序。我在ASP页面中有控件,只要服务器通知我状态更改,我就会更新。因此,例如,如果我在UpdatePanel中包含Label,我想在服务器告诉我它已更改时更改文本。我有一个回调函数,只要服务器向我发送如下所示的数据,就会调用它:
private void DataReceived(字符串数据)
{
labelStatus.Text = ParseData(数据);
}
ParseData只获取所需的文本的数据。但是,Label没有更新,因为没有PostBack。
我尝试过:
如果我将数据存储在一个静态变量中并运行一个计时器来不断检查是否有任何未处理的数据可用,它将起作用,因为计时器触发一个PostBack事件。虽然它有效,但它是不受欢迎的,因为即使没有任何变化,页面也会不断刷新。
有没有办法在不使用计时器的情况下更新标签?
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?
推荐答案
你可以做一个部分页面回发或您可以使用Javascript将数据发布到场景后面的服务器。
有关使用AJAX和UpdatePanels的信息,请参阅MSDN
部分 - 页面渲染概述 [ ^ ]
要使用Javascript,您需要一个Javascript计时器 - 请参考W3学校的Javascript计时器JavaScript计时事件 [ ^ ]
有关创建XMLHTTP帖子或获取的信息
AJAX向服务器发送XMLHttpRequest [ ^ ]
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[^]
这篇关于更新asp控件而不回发事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!