如何在服务器上处理某些内容时更新状态标签?

时间:2013-04-02 21:21:22

标签: asp.net ajax updatepanel

当用户单击按钮时,我需要处理(服务器端)大量数据(文件)。我想在处理时显示每个文件名的运行摘要。我一直在尝试使用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>

有没有办法让这项工作?或者更好的方法呢?

提前致谢, 杰森

1 个答案:

答案 0 :(得分: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();
    }


}