我有一個asp.net網站,其中包含一個線程,從WCF服務中獲取一些數據。該線程以無限循環運行,等待每次運行1秒鐘。現在我想在標籤中顯示它從WCF服務中獲得的東西。我將該標籤添加到UpdatePanel並調用.Update()方法。我沒有得到任何例外,但是,標籤根本沒有更新。這裏是我的代碼(簡化):asp.net UpdatePanel不更新
t = new Thread(new ThreadStart(() =>
{
while (true)
{
Label1.Text = GetFromWCF() + " " + DateTime.Now.ToString();
updatePanel.Update();
Thread.Sleep(1000);
}
}
));
t.IsBackground = true;
t.Start();
此代碼是在頁面的OnInit方法。 updatePanel看起來像這樣:
<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>
我是否缺少任何東西?也許我還應該告訴你,我對asp.net非常陌生。
謝謝你的提示!我現在看到這是如何完成的。我沒有在一個線程中存在無限循環,而是每秒都有一個計時器。這個計時器調用更新,現在它工作! – Christian