【问题标题】:How to manage multi threading in asp.net to fill the labels?如何在 asp.net 中管理多线程以填充标签?
【发布时间】:2012-06-04 07:10:59
【问题描述】:

我的 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;
            }

我收到两个回复,我想显示我得到的第一个回复而不等待第二个回复,在获得第二个回复后应该立即显示而不会丢失第一个回复,请给我任何紧急建议, 有没有其他程序可以得到这样的输出

谢谢 赫曼斯

【问题讨论】:

  • 每次我在网络应用程序中看到Thread.Sleep,我都会开始踢小狗。尝试在客户端进行;从服务器更新页面并不值得你费尽心思去完成它,如果它甚至可以完成的话。

标签: c# asp.net multithreading c#-4.0 asynchronous


【解决方案1】:

您不能在服务器代码中进行这样的延迟。在服务器代码呈现页面之前,页面不会发送到浏览器,这发生在控制事件之后。代码将等待 7 秒钟,然后呈现页面并将其发送到浏览器。

您必须使用客户端代码来获得您所追求的体验。服务器在发送到浏览器后无法将更改推送到网页。

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);
}

【讨论】:

  • 请告诉我关于我修改后的问题的任何想法
  • @hmkmudiam:请不要更改问题,这会使现有答案非常混乱。如果您有一个密切相关的问题,您可以将其添加到问题中,否则您应该将其作为一个新问题。
  • 好的,谢谢你的建议,请给我关于我的问题的建议
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-03-11
  • 1970-01-01
  • 2020-09-10
  • 1970-01-01
  • 2013-06-18
  • 1970-01-01
  • 2015-02-26
相关资源
最近更新 更多