C# 如何在asp.net中管理多线程以填充标签?
在我的aspx页面中有一个按钮和两个标签,我想在标签中显示文本,几秒钟后,我想在点击按钮时用不同的文本填充第二个标签 我的代码是 源文件C# 如何在asp.net中管理多线程以填充标签?,c#,asp.net,multithreading,c#-4.0,asynchronous,C#,Asp.net,Multithreading,C# 4.0,Asynchronous,在我的aspx页面中有一个按钮和两个标签,我想在标签中显示文本,几秒钟后,我想在点击按钮时用不同的文本填充第二个标签 我的代码是 源文件 <asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional"> <ContentTemplate> <asp:Label ID="lblFirst" runat="server" Text=""></asp:Label>
<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;
}
我收到两个回复,我想显示我得到的第一个回复,而不等待第二个回复,在得到第二个回复后,应立即显示,而不失去第一个回复,请给我任何紧急建议,
是否有任何其他程序可获得此类输出
谢谢
hemanth您不能在服务器代码中进行这样的延迟。在服务器代码呈现该页面之前,该页面不会发送到浏览器,这是在控件事件之后发生的。代码只需等待七秒钟,然后呈现页面并将其发送到浏览器
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);
}
您必须使用客户端代码来获得所需的体验。将网页发送到浏览器后,服务器无法将更改推送到网页
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);
}
在服务器代码中不能进行这样的延迟。在服务器代码呈现该页面之前,该页面不会发送到浏览器,这是在控件事件之后发生的。代码只需等待七秒钟,然后呈现页面并将其发送到浏览器
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);
}
您必须使用客户端代码来获得所需的体验。将网页发送到浏览器后,服务器无法将更改推送到网页
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);
}
每次我在网络应用程序中看到
Thread.Sleep
,我就开始踢小狗。试着用客户端来代替;如果可以的话,从服务器上更新页面并不值得你费那么多功夫。每次我在web应用程序中看到Thread.Sleep
,我就开始踢小狗。试着用客户端来代替;如果可以的话,从服务器上更新页面不值得你费尽心思去做。请告诉我关于我修改过的页面的任何想法question@hmkmudiam当前位置请不要更改问题,这会使现有答案非常混乱。如果你有一个密切相关的问题,你可以把它添加到问题中,否则你应该把它变成一个新问题。好的,谢谢你的建议,请给我关于我的问题的建议question@hmkmudiam当前位置请不要更改问题,这会使现有答案非常混乱。如果你有一个密切相关的问题,你可以把它添加到问题中,否则你应该把它变成一个新问题。好的,谢谢你的建议,请给我关于我的问题的建议