【问题标题】:Updating ASP.NET UpdatePanel during processing loop在处理循环期间更新 ASP.NET UpdatePanel
【发布时间】:2014-12-20 18:44:16
【问题描述】:

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

我有一个带有 UpdatePanel 的 ASP.NET 页面。 UpdatePanel 中有一个按钮和一个多行文本框。按钮的单击事件会禁用按钮并进入一个处理循环,该循环会多次更新 TextBox 的 Text 属性。但是,当然,直到循环完成,TextBox 才真正更新。

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

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

这是我的页面代码:

<!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 值的计时器,则该代码在循环完成之前永远不会触发。

【问题讨论】:

  • 你想追加文本吗?
  • Mairaj - 是的,我想在文本框中附加文本,指示在进程进行时向用户显示进度。它现在所做的是仅在该过程完成后才在 TextBox 中显示文本。

标签: c# asp.net updatepanel


【解决方案1】:

嗯,我终于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;
            }
        }

    }
}

【讨论】:

    【解决方案2】:

    你在找这个吗

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

    【讨论】:

    • 没有。文本已附加到 ViewState 值中,因此针对 TextBox 更改运算符没有任何区别。我要解决的问题是用户在此过程中没有在 TextBox 中看到更新。 TextBox1 只有在处理循环完成后才会更新。
    猜你喜欢
    • 2012-12-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多