gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 14:29:00 25 4
gpt4 key购买 nike

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

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

我看到了向页面添加 Timer 控件的建议,并且我已经尝试过了。但是 Timer's 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,则该代码在循环完成之前不会触发。

最佳答案

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

}
}

关于c# - 在处理循环期间更新 ASP.NET UpdatePanel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27583227/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com