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

标签 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>

有办法让这个工作成功吗?或者也许有更好的方法?

提前致谢, 杰森

最佳答案

您需要使用 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();
    }


}

关于asp.net - 如何在服务器上处理某些内容时更新状态标签?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15774629/

相关文章:

javascript - Page.ResolveUrl 在 JavaScript 中不起作用

c# - 从 Bootstrap 模式弹出窗口提交数据

jquery - Jasmine 2集成测试

c# - 在 UpdatePanel 之外更改 TextBox 中的文本

asp.net - 自定义角色提供程序 web.config 错误

asp.net - 如何从ashx文件获取QueryString?

javascript - 如何在 javascript 中计算 gridview 中的行数?

javascript - jCarousel ajax 加载下一个按钮

asp.net - 如何使用CSS在 GridView 的中心显示UpdateProgress?

asp.net - 是否可以在 javascript 中进行两个 .click 方法调用