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

时间:2014-12-20 18:44:17

标签: c# asp.net updatepanel

我已经多次看到这个特殊问题,但我见过的解决方案都没有实现我想要做的事情。

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

我已经看到了向页面添加Timer控件的建议,我试过了。但是Timer的Tick在循环完成之前不会触发,所以它没用!

任何人都有这方面的解决方案吗?我需要一个页面,允许用户单击按钮以启动处理数据库中多个记录的进程,并向用户发出更新,指示每个记录已处理。

这是我的页面代码:

<!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值的Timer,那么该代码永远不会触发,直到循环完成。

2 个答案:

答案 0 :(得分:6)

好吧,我终于this post找到了答案。我已经相应地调整了我自己的代码(进行了一些调整),现在效果很好。

这是我的网页代码:

<%@ 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;
            }
        }

    }
}

答案 1 :(得分:0)

你在找这个吗?

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

    }