在处理循环期间更新 ASP.NET 更新面板

本文关键字:更新 ASP NET 处理 循环 | 更新日期: 2023-09-27 18:34:52

我已经多次看到这个特定问题发布,但我看到的解决方案都没有真正完成我想要做的事情。

我有一个 ASP.NET 页面,上面有一个更新面板。 在更新面板中是一个按钮和一个多行文本框。 按钮的单击事件禁用按钮并进入多次更新 TextBox 的 Text 属性的处理循环。 但是,当然,在循环完成之前,文本框实际上不会更新。

我已经看到了向页面添加计时器控件的建议,我已经尝试过了。 但是计时器的滴答声在循环完成之前不会触发,所以它没有用!

有人对此有可行的解决方案吗? 我需要一个页面,允许用户单击按钮来启动处理数据库中的多个记录并向用户提供更新,指示已处理的每条记录。

这是我的页面代码:

<!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:ScriptManager ID="ScriptManager1" runat="server">
        </asp:ScriptManager>
        <br />
        <asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Always">
            <ContentTemplate>
                <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
                <br />
                <asp:TextBox ID="TextBox1" runat="server" Height="230px" TextMode="MultiLine" 
                    Width="800px"></asp:TextBox>
            </ContentTemplate>
        </asp:UpdatePanel>
    </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;
namespace UpdatePanel
{
    public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            Button1.Enabled = false;
            for (int x = 1; x < 6; x++)
            {
                ViewState["progress"] += "Beginning Processing Step " + x.ToString() + " at " + DateTime.Now.ToLongTimeString() + "..." + System.Environment.NewLine;
                System.Threading.Thread.Sleep(1000); //to simulate a process that takes 1 second to complete.
                ViewState["progress"] += "Completed Processing Step " + x.ToString() + " at " + DateTime.Now.ToLongTimeString() + System.Environment.NewLine;
                TextBox1.Text = ViewState["progress"].ToString();
            }
        }
    }
}

如果我只是在循环中设置 VewState 值,然后添加一个将 TextBox Text 属性设置为 ViewState 值的计时器,则该代码在循环完成之前永远不会触发。

在处理循环期间更新 ASP.NET 更新面板

好吧,我终于在这篇文章中找到了答案。 我已经相应地调整了自己的代码(进行了一些调整(,现在它运行良好。

这是我的页面代码:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="UpdatePanel.Default" %>
<!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:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
        <asp:Timer runat="server" ID="Timer1" Interval="1000" Enabled="false" ontick="Timer1_Tick" />
        <br />
        <asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Always">
            <Triggers>
                <asp:AsyncPostBackTrigger ControlID="Timer1" EventName="Tick" />
            </Triggers>
            <ContentTemplate>
                <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
                <br />
                <asp:TextBox ID="TextBox1" runat="server" Height="250px" TextMode="MultiLine" 
                    Width="800px"></asp:TextBox>
            </ContentTemplate>
        </asp:UpdatePanel>
    </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.Threading;
namespace UpdatePanel
{
    public partial class Default : System.Web.UI.Page
    {
        protected static string content;
        protected static bool inProcess = false;
        protected static bool processComplete = false;
        protected static string processCompleteMsg = "Finished Processing All Records.";
        protected void Page_Load(object sender, EventArgs e){ }
        protected void Button1_Click(object sender, EventArgs e)
        {
            Button1.Enabled = false;
            Timer1.Enabled = true;
            Thread workerThread = new Thread(new ThreadStart(ProcessRecords));
            workerThread.Start();
        }
        protected void ProcessRecords()
        {
            inProcess = true;
            for (int x = 1; x <= 5; x++)
            {
                content += "Beginning Processing Step " + x.ToString() + " at " + DateTime.Now.ToLongTimeString() + "..." + System.Environment.NewLine;
                Thread.Sleep(1000);
                content += "Completed Processing Step " + x.ToString() + " at " + DateTime.Now.ToLongTimeString() + System.Environment.NewLine + System.Environment.NewLine;
            }
            processComplete = true;
            content += processCompleteMsg;
        }
        protected void Timer1_Tick(object sender, EventArgs e)
        {
            if (inProcess)
                TextBox1.Text = content;
            int msgLen = processCompleteMsg.Length;
            if (processComplete && TextBox1.Text.Substring(TextBox1.Text.Length - processCompleteMsg.Length) == processCompleteMsg) //has final message been set?
            {
                inProcess = false;
                Timer1.Enabled = false;
                Button1.Enabled = true;
            }
        }
    }
}

你在寻找这个吗

protected void Button1_Click(object sender, EventArgs e)
    {
        Button1.Enabled = false;
        for (int x = 1; x < 6; x++)
        {
            ViewState["progress"] += "Beginning Processing Step " + x.ToString() + " at " + DateTime.Now.ToLongTimeString() + "..." + System.Environment.NewLine;
            System.Threading.Thread.Sleep(1000); //to simulate a process that takes 1 second to complete.
            ViewState["progress"] += "Completed Processing Step " + x.ToString() + " at " + DateTime.Now.ToLongTimeString() + System.Environment.NewLine;
            TextBox1.Text += ViewState["progress"].ToString();
        }
    }