2012-06-04 27 views
0

我有一個按鈕,並在我的aspx頁面的兩個標籤,我想在標籤顯示文本和幾秒鐘後,我想,以填補第二標籤與按鈕不同的文字點擊 我的代碼是 源文件如何在asp.net中管理多線程來填充標籤?

<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

+1

每當我在一個web應用程序中看到'Thread.Sleep',我就開始踢小狗。嘗試做它的客戶端,而不是;如果甚至可以完成,那麼從服務器更新頁面並不值得你必須經歷的所有麻煩。 – cHao

回答

3

你不能讓一個延遲就像服務器代碼中那樣。在服務器代碼已經呈現之前,該頁面不會發送到瀏覽器,並且發生在控件事件之後。代碼將等待七秒鐘,然後渲染頁面並將其發送到瀏覽器。

您必須使用客戶端代碼才能獲得您之後的體驗。服務器在發送到瀏覽器後無法將更改推送到網頁。

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

請給我任何關於我修改的問題的想法 – hmk

+1

@hmkmudiam:請不要改變這個問題,這會使現有的答案非常混亂。如果你有一個密切相關的問題,你可以把它添加到問題中,否則你應該只是把它作爲一個新的問題。 – Guffa

+0

好的,謝謝你的建議,請給我關於我的問題的建議 – hmk