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>

有没有办法使这项工作?或者也许有更好的方法来做到这一点?

在此先感谢,杰森

4

1 回答 1

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>

进程.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();
    }


}
于 2013-04-03T03:39:55.427 回答