gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 05:03:48 25 4
gpt4 key购买 nike

当用户单击按钮时,我需要处理(服务器端)大量数据(文件)。我想显示正在处理的每个文件名的运行摘要。我一直在尝试使用 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/

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