2011-03-11 149 views
2

我有一個運行時間很長的進程,我希望隨着進程的進行更新頁面上的標籤,但我沒有任何運氣。ASP.NET異步標籤更新

這裏的ASPX:

<%@ Page Language="C#" Async="true" AutoEventWireup="true" CodeFile="Async.aspx.cs" Inherits="Website.structureDoc.Async" %> 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 

<html xmlns="http://www.w3.org/1999/xhtml" > 
<head runat="server"> 
    <title></title> 
</head> 
<body> 
    <form id="form1" runat="server"> 
    <div> 
     <asp:Button ID="startAsyncButton" runat="server" Text="Run" onclick="startAsyncButton_Click"/> 
     <asp:Button ID="cancelAsyncButton" runat="server" Text="Cancel" onclick="cancelAsyncButton_Click"/> 

     <asp:label id="resultLabel" runat="server"></asp:label> 

    </div> 
    </form> 
</body> 
</html> 

而這裏的代碼背後:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using System.ComponentModel; 

namespace Website.structureDoc 
{ 
    public partial class Async : System.Web.UI.Page 
    { 
     BackgroundWorker backgroundWorker1; 
     protected void Page_Load(object sender, EventArgs e) 
     { 
      backgroundWorker1 = new BackgroundWorker(); 

      backgroundWorker1.WorkerReportsProgress = true; 
      backgroundWorker1.WorkerSupportsCancellation = true; 
     } 

     protected void startAsyncButton_Click(object sender, EventArgs e) 
     { 
      if (backgroundWorker1.IsBusy != true) 
      { 
       // Start the asynchronous operation. 
       backgroundWorker1.RunWorkerAsync(); 
      } 
     } 

     protected void cancelAsyncButton_Click(object sender, EventArgs e) 
     { 
      if (backgroundWorker1.WorkerSupportsCancellation == true) 
      { 
       // Cancel the asynchronous operation. 
       backgroundWorker1.CancelAsync(); 
      } 
     } 

     // This event handler is where the time-consuming work is done. 
     private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e) 
     { 
      BackgroundWorker worker = sender as BackgroundWorker; 

      for (int i = 1; i <= 10; i++) 
      { 
       if (worker.CancellationPending == true) 
       { 
        e.Cancel = true; 
        break; 
       } 
       else 
       { 
        // Perform a time consuming operation and report progress. 
        System.Threading.Thread.Sleep(500); 
        worker.ReportProgress(i * 10); 
       } 
      } 
     } 

     // This event handler updates the progress. 
     private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e) 
     { 
      resultLabel.Text = (e.ProgressPercentage.ToString() + "%"); 
     } 

     // This event handler deals with the results of the background operation. 
     private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) 
     { 
      if (e.Cancelled == true) 
      { 
       resultLabel.Text = "Canceled!"; 
      } 
      else if (e.Error != null) 
      { 
       resultLabel.Text = "Error: " + e.Error.Message; 
      } 
      else 
      { 
       resultLabel.Text = "Done!"; 
      } 
     } 
    } 
} 

都是後臺的工人不是這個正確的方法?

如果可能的話,我寧願不使用AJAX來做到這一點。

回答

7

這是行不通的。問題在於,asp.net在請求/響應模型上工作。爲了接收任何更新,客戶端(瀏覽器)需要從服務器請求信息。爲了更新您的標籤,您需要向服務器發送一個新值的請求,然後根據該值來確定該值,並以適當的值迴應您的顯示。

最簡單的方法是使用AJAX以某種方式爲數據請求計時,或者改爲設置計時器並使頁面在設置的計時器上自行刷新。底線是您需要頁面輪詢更新的值,僅僅因爲您更新工作進程中的值並不意味着瀏覽器將收到此新值。

我知道你說你不想使用AJAX,但看看下面的jQuery.get()以更好地理解你需要做什麼。