如何在 asp.net 中管理多线程以填充标签
本文关键字:多线程 填充 标签 管理 asp net | 更新日期: 2023-09-27 18:36:15
我的aspx页面中有一个按钮和两个标签,我想在标签中显示文本,几秒钟后我想在按钮单击时用不同的文本填充第二个标签我的代码是源文件
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:Label ID="lblFirst" runat="server" Text=""></asp:Label>
<asp:Label ID="lblSecond" runat="server" Text=""></asp:Label>
<asp:Button ID="btnFirst" runat="server" Text="First"
onclick="btnFirst_Click" />
</ContentTemplate>
</asp:UpdatePanel>
和代码文件
protected void btnFirst_Click(object sender, EventArgs e)
{
first();
second();
}
private void first()
{
I am Calling a method form my class file which returns a string , and assigning to label first
//class1 obj=new class1();
//string result=obj.first()
// lblFirst.Text =result;
}
private void second()
{
I am Calling a method form my class file which returns a string , and assigning to label Second
//class2 obj=new class2();
//string result1=obj.Second()
lblSecond.Text = result1;
}
我收到了两个回复,我想显示我首先得到的回复而不等待第二个回复,在得到第二个回复后应该立即显示而不会丢失第一个回复,请给我任何紧急建议, 是否有任何其他过程可以获取这样的输出
谢谢赫曼斯
您不能在服务器代码中产生这样的延迟。在服务器代码呈现页面之前,该页不会发送到浏览器,这发生在控件事件之后。代码将等待七秒钟,然后呈现页面并将其发送到浏览器。
您必须使用客户端代码来获得您所追求的体验。将更改发送到浏览器后,服务器无法将更改推送到网页。
protected void btnFirst_Click(object sender, EventArgs e) {
string code =
"window.setTimeout(function(){document.getElementById('" + lblFirst.ClientID + "').innerHTML='first filled'},1000);"+
"window.setTimeout(function(){document.getElementById('" + lblSecond.ClientId + "').innerHTML='Second filled'},7000);";
Page.ClientScript.RegisterStartupScript(this.GetType(), "messages", code, true);
}