通过点击表单上的c#asp.net按钮,如何在继续下一行代码之前执行一行代码?
使用c代码,我在网页上有一个按钮,可以运行PowerShell进程(请参阅下面的代码)。这个过程需要一段时间才能运行,因此当我单击按钮时,我希望在标签中显示一条消息,说明该过程正在运行。然后,一旦流程完成,将标签消息更改为流程已完成。我目前的代码是:通过点击表单上的c#asp.net按钮,如何在继续下一行代码之前执行一行代码?,c#,asp.net,C#,Asp.net,使用c代码,我在网页上有一个按钮,可以运行PowerShell进程(请参阅下面的代码)。这个过程需要一段时间才能运行,因此当我单击按钮时,我希望在标签中显示一条消息,说明该过程正在运行。然后,一旦流程完成,将标签消息更改为流程已完成。我目前的代码是: protected void button_Click(object sender, EventArgs e) { label.Text = "Running Process...";
protected void button_Click(object sender, EventArgs e)
{
label.Text = "Running Process...";
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = @"powershell.exe";
startInfo.Arguments = @"C:\PowerShell\script.ps1";
startInfo.RedirectStandardOutput = true;
startInfo.RedirectStandardError = true;
startInfo.UseShellExecute = false;
startInfo.CreateNoWindow = true;
Process process = new Process();
process.StartInfo = startInfo;
process.Start();
label.Text = "Finished Process. ";
}
当前不会显示“正在运行进程…”的初始消息。它只是运行整个代码并显示“Finished Process”消息
因此,我需要一种方法来提交第一行代码以填充标签,然后继续处理代码的其余部分,但我仍然无法确定如何做到这一点
谢谢你的帮助
约翰纳森。你可以用它来完成这件事
您可以使用jquery/ajax发布它,在流程开始时设置所需的消息,并在流程结束时设置完成消息。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>
//您的流程代码
SB.追加(db.Name+“BackUp Complted!”)
//您的流程代码
SB.追加(“
”);
}
//全部完成!
UpdateProgress(100,“所有数据库备份完成!”);
}
捕获(例外情况除外)
{
UpdateProgress(0,“异常:+ex.Message”);
某人追加(“备份失败!”);
SB.追加(“
”);
SB.Append(“失败的数据库:+DBName”);
SB.追加(“
”);
SB.追加(“例外:+ex.Message”);
}
}
受保护的void UpdateProgress(完成两个百分点,字符串消息)
{
//写出父脚本回调。
Write(String.Format(“parent.UpdateProgress({0},{1}”);”,PercentComplete,Message));
//确保响应未在服务器上缓冲。
Response.Flush();
}
}
看看asp生命周期。如果不回发,则无法通过服务器端代码更改页面。这就是单击asp按钮时所做的操作,等等
如果要显示加载指示器,必须在运行长时间运行的代码之前创建它。否则,该指示器将永远不会向用户显示,因为用户不会收到新页面
这里你有两个选择-做一次回发放置加载指示器,再做一次回发(我不建议这样做),或者只是通过JS显示一些东西
--偷自
这将显示服务器端代码正在运行的加载指示器。您可以尝试添加内联javascript代码以更改标签
<asp:Button ID="button" runat="server" OnClick="button_Click" Text="Click"
OnClientClick="this.value = 'Running Process...'" />
这将在您单击按钮时出现,然后标签将更改为“已完成流程”。当页面重新加载时。您可以在ajax上执行此操作
function ShowProcessing()
{
$('#label').text('Running Process...');
// call pagemethod with your functionality
// Let's success callback is OnSuccess
}
function OnSuccess()
{
$('#label').text('Finished Process.');
}
当前进程大约需要多少时间?这是回发,可能需要显示正在运行的进程。。。客户端点击消息(javascript)嘿,您可以使用
更新面板
,为此,您需要阅读有关ASP.net客户端/服务器模型的更多信息。这就是泄漏的抽象泄漏的地方。感谢您的回复。javascript和更新面板实际上都对我有用,所以谢谢:)这对我有用,我以前不知道AJAX,所以谢谢它看起来很有用。最后对我来说,最简单的答案是添加一行简单的内联javascript,正如Yuliam Chandra所回答的。谢谢你,这对我来说非常有效。关于这个问题,有几个有用且有效的答案,但这对我来说是最快和最简单的:)@Johnathan,很高兴它有帮助,这是带有回发事件的asp.net的本质,ajax将主要用于asp.mvc,您可能需要开始适应它:)
<asp:Button ID="button" runat="server" OnClick="button_Click" Text="Click"
OnClientClick="this.value = 'Running Process...'" />
function ShowProcessing()
{
$('#label').text('Running Process...');
// call pagemethod with your functionality
// Let's success callback is OnSuccess
}
function OnSuccess()
{
$('#label').text('Finished Process.');
}