2013-04-02 15 views
2

我有一個要求,當用戶點擊一個按鈕時處理(服務器端)大量的數據(文件)。我想在處理每個文件名時顯示正在運行的摘要。我一直試圖用UpdatePanel控件來做,但只有最後一次更新發生。下面是我創建模擬問題,一些簡單的代碼(它應該從1數到10,而是等待5秒,輸出10):如何在處理服務器上的某些內容時更新狀態標籤?

<%@ Page Language="C#" %> 

<!DOCTYPE html> 

<html xmlns="http://www.w3.org/1999/xhtml"> 
<head runat="server"> 
    <title></title> 
    <script runat="server"> 
     protected void Page_Load(object sender, EventArgs e) 
     { 
      ScriptManager1.RegisterAsyncPostBackControl(Button1); 
     } 

     protected void Button1_Click(object sender, EventArgs e) 
     { 
      for (int i = 1; i <= 10; i++) 
      { 
       Label1.Text = i.ToString(); 
       System.Threading.Thread.Sleep(500); 
       UpdatePanel1.Update(); 
      } 
     } 
    </script> 
</head> 
<body> 
    <form id="form1" runat="server"> 
    <div> 
     <asp:ScriptManager ID="ScriptManager1" runat="server"> 
     </asp:ScriptManager> 
     <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" /> 
     <asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional"> 
      <ContentTemplate> 
       <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label> 
      </ContentTemplate> 
     </asp:UpdatePanel> 
    </div> 
    </form> 
</body> 
</html> 

有沒有一種方法,使這項工作?或者,也許更好的方法來做到這一點?

在此先感謝,傑森

回答

3

你需要使用一個AJAX調用服務器。我從我之前的一個項目中複製了這段代碼,它有點長的代碼。試試吧,讓我知道它是否有效。

page1.aspx這個

<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server"> 

    <script type="text/javascript"> 

     function BeginProcess() { 
      // Create an iframe. 
      var iframe = document.createElement("iframe"); 

      // Point the iframe to the location of 
      // the long running process. 
      iframe.src = "Process.aspx"; 

      // Make the iframe invisible. 
      iframe.style.display = "none"; 

      // Add the iframe to the DOM. The process 
      // will begin execution at this point. 
      document.body.appendChild(iframe); 

      // Disable the button and blur it. 
      document.getElementById('trigger').blur(); 
     } 

     function UpdateProgress(PercentComplete, Message) { 
      document.getElementById('ContentPlaceHolder2_lbDownload').setAttribute("disabled", "true"); 
      document.getElementById('trigger').value = PercentComplete + '%: ' + Message; 

     } 


    </script> 
</asp:Content> 
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder2" Runat="Server"> 
    <asp:UpdatePanel ID="UpdatePanel1" runat="server"> 
    <ContentTemplate> 

    <input type="submit" value="Start BackUp Process!" 
    id="trigger" onclick="BeginProcess(); return false;" 
    style="width: 250px;" /> 

    </ContentTemplate> 
    </asp:UpdatePanel> 


    <asp:UpdateProgress ID="UpdateProgress1" 
    AssociatedUpdatePanelID="UpdatePanel1" runat="server"> 
<ProgressTemplate> 
     </ProgressTemplate> 
</asp:UpdateProgress> 


</asp:Content> 

Process.aspx.cs

public partial class Process : System.Web.UI.Page 
{ 
    protected void Page_Load(object sender, EventArgs e) 
    { 

     StringBuilder SB = new StringBuilder(); 
     // Padding to circumvent IE's buffer. 
     Response.Write(new string('*', 256)); 
     Response.Flush(); 

     // Initialization 
     UpdateProgress(0, "Initializing task."); 


     try 
     { 


      foreach (yourloophere) 
      { 

         UpdateProgress(increment, db.Name + " Backup Started...."); 
       //your process code 
         UpdateProgress(increment, db.Name + " Backup Completed!"); 
    //your process code 
         SB.Append(db.Name + "BackUp Complted!"); 

     //your process code 
       SB.Append("<br/>"); 



      } 


      // All finished! 
      UpdateProgress(100, "All Database BackUp Completed!"); 

     } 
     catch (Exception ex) 
     { 
      UpdateProgress(0, "Exception: " + ex.Message); 

      SB.Append("Back Up Failed!"); 
      SB.Append("<br/>"); 
      SB.Append("Failed DataBase: " + DBName); 
      SB.Append("<br/>"); 
      SB.Append("Exception: " + ex.Message); 

     } 


    } 


    protected void UpdateProgress(double PercentComplete, string Message) 
    { 
     // Write out the parent script callback. 
     Response.Write(String.Format("<script type=\"text/javascript\">parent.UpdateProgress({0}, '{1}');</script>", PercentComplete, Message)); 
     // To be sure the response isn't buffered on the server.  
     Response.Flush(); 
    } 


} 
+0

哇,這絕對是更復雜的比我希望的。由於這是功能,我只打算使用一次然後扔掉(從舊系統遷移到新的數據)我決定現在不擔心它。感謝您的答覆,但可以在未來派上用場。 – Jason

相關問題