ala*_*ala 6 asp.net user-interface multithreading
我有一个包含以下内容的ASP.NET网站
<asp:UpdatePanel ID="UpdatePanel1" runat="server" >
<ContentTemplate>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
</ContentTemplate>
</asp:UpdatePanel>
Run Code Online (Sandbox Code Playgroud)
我创建了一个线程函数.在执行此功能期间,我想更新用户界面上的一些控件.
protected void Page_Load(object sender, EventArgs e)
{
new Thread(new ThreadStart(Serialize_Click)).Start();
}
protected void Serialize_Click()
{
for (int i = 1; i < 10; i++)
{
Label1.Text = Convert.ToString(i);
UpdatePanel1.Update();
System.Threading.Thread.Sleep(1000);
}
}
Run Code Online (Sandbox Code Playgroud)
如何在线程执行期间更新Web控件?我是否需要强制"UpdatePanel1"进行回发?怎么样?
您需要使用客户端计时器(或其他方法)让浏览器向服务器请求更新,例如以下简化示例:
<asp:UpdatePanel ID="up" runat="server">
<ContentTemplate>
<asp:Timer ID="Timer1" runat="server" Interval="1000" OnTick="timer_Ticked" />
<asp:Label ID="Label1" runat="server" Text="1" />
</ContentTemplate>
</asp:UpdatePanel>
Run Code Online (Sandbox Code Playgroud)
然后在你的代码隐藏中:
protected void timer_Ticked(object sender, EventArgs e)
{
Label1.Text = (int.Parse(Label1.Text) + 1).ToString();
}
Run Code Online (Sandbox Code Playgroud)
如果您的后台进程正在更新某个状态,则您需要将共享状态存储在会话,http缓存或数据库中.请注意,由于许多因素,缓存可能会过期,如果IIS回收应用程序池,后台线程可能会被杀死.
| 归档时间: |
|
| 查看次数: |
7296 次 |
| 最近记录: |