2014-04-03 17 views
1

我有一個簡單的應用程序,使用VS2012在C#中編寫,在Web窗體應用程序中。使用ASP.Net在點擊事件期間多次更新標籤文本

有一個按鈕開始處理,我想通過在Label中放置一些文本來給用戶更新處理進度。我想我可以將Label控件放入UpdatePanel中,然後在Button單擊事件期間通過調用UpdatePanel Update()方法觸發Label文本中的更改。但是,標籤文本發生更改的唯一時間是點擊事件完成並顯示標籤文本後,「處理完成」。

這是我的xml:


這是後面的代碼:

protected void btnCreateFiles_Click(object sender, EventArgs e) 
    { 
     //Do some stuff 

     //Show the message that the application is processing 
     lblCaption.Text = "Processing...updating label data"; 
     ProgressUpdatePanel.ContentTemplateContainer.Controls.Add(lblCaption); 
     ProgressUpdatePanel.Update();  

     //Call to database to insert labels 
     //Call to database to get the labels 

     lblCaption.Text = "Processing...creating label files."; 
     ProgressUpdatePanel.ContentTemplateContainer.Controls.Add(lblCaption); 
     ProgressUpdatePanel.Update(); 

     //Create Text files from data 

     lblCaption.Text = "Processing...updating label counts."; 
     ProgressUpdatePanel.ContentTemplateContainer.Controls.Add(lblCaption); 
     ProgressUpdatePanel.Update(); 

     //Database call to insert count records 
     //Create file of count records 

     lblCaption.Text = "Processing...completed."; 
     ProgressUpdatePanel.ContentTemplateContainer.Controls.Add(lblCaption); 
     ProgressUpdatePanel.Update(); 
    } 

顯示的純文本是最後更新......」處理...完成「。

爲什麼其他更新不會觸發要更改的標籤文本?

感謝

更新的要求

我已經改變了我的要求一點。而不是多次更新。我在進度模板中添加了一個標籤,以便在處理開始時顯示一條初始消息,然後在處理完成時使用lblCaption標籤顯示另一條消息。但是,如果再次單擊該按鈕,則會顯示以下兩條消息:'請稍等,正在創建文件...'和'處理完成'。

如何將進度模板中的文本重置爲僅顯示「請稍候...」消息而不是「正在處理...完成」消息?

這是現在的aspx文件:


現在的按鈕事件處理函數方法是這樣的:

protected void btnCreateFiles_Click(object sender, EventArgs e) 
     { 
      //All of the processing is done here... 

      //This works correctly the first time a user click the button 
      //But the second time, this text remains and the 'Please wait...' text from the lblProgress label 
      // is added above this text. 
      ProgressUpdatePanel.ContentTemplateContainer.Controls.Add(lblCaption); 
      ProgressUpdatePanel.Update(); 
      lblCaption.Text = "Processing...completed."; 

     } 

UPDATE 我嘗試了以下操作來清除標籤中的文本lblCaption。我已通過代碼執行此代碼並執行標籤文本未被清除。
我試圖在Page_Load()方法正在重置標記,如下所示:

protected void Page_Load(object sender, EventArgs e) 
    { 
     if (!IsPostBack) 
     { 
      txtBoxEnvironment.Text = CurrentEnvironment; 
      DAL.setConnectionString(CurrentEnvironment); 
     } 
     ProgressUpdatePanel.ContentTemplateContainer.Controls.Add(lblCaption); 
     ProgressUpdatePanel.Update(); 
     lblCaption.Text = ""; 
    } 

我已刪除了ProgressUpatePanel方法並試圖只設置在標籤的文本和它不被複位。

我錯過了什麼?

+0

請出示相關的客戶端代碼。 –

+0

我唯一的客戶端代碼是在上面顯示的xml中。我沒有任何javascript等。 –

回答

2

UpdatePanel上的Update方法無法按照您的預期方式工作。

來自MSDN http://msdn.microsoft.com/en-us/library/system.web.ui.updatepanel.update(v=vs.110).aspx: 如果您有服務器代碼必須執行以確定是否應更新UpdatePanel控件,請調用Update方法。

如果你想模擬一個進程的進展,你可以通過調用隱藏按鈕來觸發多個部分回發。每個按鈕都會執行另一步,並通過javascript調用下一個按鈕。這可能容易出錯並且效率低下。但是有一種方法可以做你想做的事情。 另一個將使用JQuery和(仍然打幾個電話)使用$ .ajax,調用成功回調的下一步。

使用你的代碼作爲一個例子,這就是你想要什麼樣的基本工作的例子 - 順便我的理解:

標記

<asp:UpdatePanel ID="ProgressUpdatePanel" runat="server" UpdateMode="Conditional"> 
    <ContentTemplate> 
     <asp:Button ID="btnCreateFiles" runat="server" Text="Go" OnClick="btnCreateFiles_Click" /> 
     <asp:Label ID="lblCaption" runat="server" Text="" /> 
    </ContentTemplate> 
</asp:UpdatePanel> 

<script type="text/javascript"> 
    $(document).ready(function() { 
     bindButton(); 
    }); 
    function bindButton() { 
     $('#<%=btnCreateFiles.ClientID%>').on('click', function() { 
      $('#<%=lblCaption.ClientID%>').html('Please Wait...'); 
    }); 
} 
</script> 

代碼

背後
protected void Page_Load(object sender, EventArgs e) { 
     if (!IsPostBack) { 
      //txtBoxEnvironment.Text = CurrentEnvironment; 
      //DAL.setConnectionString(CurrentEnvironment); 
     } 
     ProgressUpdatePanel.ContentTemplateContainer.Controls.Add(lblCaption); 
     ProgressUpdatePanel.Update(); 
     lblCaption.Text = ""; 

    } 

    protected void btnCreateFiles_Click(object sender, EventArgs e) { 
     //All of the processing is done here... 

     //This works correctly the first time a user click the button 
     //But the second time, this text remains and the 'Please wait...' text from the lblProgress label 
     // is added above this text. 
     ProgressUpdatePanel.ContentTemplateContainer.Controls.Add(lblCaption); 
     ProgressUpdatePanel.Update(); 
     lblCaption.Text = "Processing...completed."; 
     System.Threading.Thread.Sleep(1000); 

     ScriptManager.RegisterClientScriptBlock(Page, typeof(string), "bindButton", "bindButton();", true); 
    } 
+0

在MSDN鏈接中聲明以下內容:「UpdatePanel控件的內容在下列情況下更新:如果UpdateMode屬性設置爲Conditional,並且發生以下情況之一: 您可以調用Update方法UpdatePanel控件明確。「我多次顯式調用Update()方法,但它只在**處理完成後才更新面板**。 –

+0

正確。毫無疑問,在該方法中執行底層代碼。但是,您在單個異步HTTP請求中多次調用Update。當該請求完成時,返回的響應將包含您爲其設置屬性的最後一個狀態的HTML。在你的情況下,「處理完成」。 – InbetweenWeekends

+0

通過啓動Fiddler或Firebug並對代碼執行調試步驟,您可能會更好地瞭解我在說什麼。當你到達click事件的最後一行時,你的標籤的值將是你設置它的最後一個,這就是返回的HTML。 – InbetweenWeekends

0

我同意裏克S我們真的需要看到處理代碼;你在這裏得到的代碼不僅僅是處理過程太快而不能看到其他消息。這會導致您所看到的問題,即顯示的唯一消息是最後一條消息。

+0

我在代碼背後添加了更多細節。雖然它不相關。我做了幾個數據庫調用,並且需要時間才能完成。 –

1

我不認爲你會在沒有編寫索姆的情況下做你想做的事e額外的代碼。 UpdatePanel不會向用戶界面提供多個更新。

這篇文章可以幫助你到達那裏:http://encosia.com/easy-incremental-status-updates-for-long-requests/

這篇文章是有點老,但仍然可能會證明是有用的:http://msdn.microsoft.com/en-us/magazine/cc163393.aspx

+0

感謝您的意見和建議。我已經改變了我的一些要求。而不是多次更新。我在進度模板中添加了一個標籤,以便在處理開始時顯示一條初始消息,然後在處理完成時使用lblCaption標籤顯示另一條消息。但是,如果再次單擊該按鈕,則會顯示以下兩條消息:'請稍等,正在創建文件...'和'處理完成'。如何重置進度模板中的文本以僅顯示「請稍候...」消息? –

相關問題