我正在尝试显示由我的webapp启动的进程的控制台输出。我尝试设置它,以便当我收到一行文本时,我更新标签然后重新加载(更新)更新面板,但面板似乎没有更新。
我无法弄清楚为什么我的更新面板没有更新。我在接收时打印出控制台输出,但更新面板根本没有更新。
protected void RunBatch_Click(object sender, EventArgs e)
{
Process p = new Process();
p.StartInfo.FileName = @"C:\...\test.bat";
// Set UseShellExecute to false for redirection.
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.CreateNoWindow = true;
// Set our event handler to asynchronously read the sort output.
p.OutputDataReceived += new DataReceivedEventHandler(OutputReceived);
// Start the process.
p.Start();
// Start the asynchronous read of the sort output stream.
p.BeginOutputReadLine();
}
protected void OutputReceived(object sender, DataReceivedEventArgs e)
{
Output_lbl.Text += e.Data;
System.Diagnostics.Debug.WriteLine(Output_lbl.Text); // I see this output
UpdatePanel1.Update(); // Doesn't update
}
<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
CodeBehind="Default.aspx.cs" Inherits="Testing._Default" %>
<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<p>
<asp:Button ID="RunBatch" runat="server" Text="Run Batch!"
onclick="RunBatch_Click" />
</p>
<asp:ScriptManager runat="server" />
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional" >
<ContentTemplate>
<asp:Label ID="Output_lbl" runat="server" Text="Label"></asp:Label>
</ContentTemplate>
</asp:UpdatePanel>
</asp:Content>
编辑:
我更改了更新面板的UpdateMode。
我试图实现一个计时器来更新它所做的更新面板,但它打印出“1:48:07 PM - ”...并继续计数而不打印任何其他Text
甚至之后收到调试消息:
This is the output from the batch file
This is the output from the batch fileThis is output line 2 from the batch file
This is the output from the batch fileThis is output line 2 from the batch fileThis is output line 3 from the batch file
string Text = "";
protected void OutputReceived(object sender, DataReceivedEventArgs e)
{
Text += e.Data;
System.Diagnostics.Debug.WriteLine(Text);
}
protected void Timer1_Tick(object sender, EventArgs e)
{
Output_lbl.Text = DateTime.Now.ToLongTimeString() + " -- " + Text;
}
答案 0 :(得分:2)
您的面板不会更新,因为您将UpdateMode设置为Conditional而未指定任何会导致它以异步方式回发到服务器并获取更新内容的触发器。您需要在UpdatePanel中嵌入一个Timer,并将其配置为UpdatePanel的触发器。请参阅文章How to refresh update panel with a timer.
为什么不尝试将AJAX与Web方法结合使用,而不是使用更新面板?众所周知,UpdatePanel很难处理。
编辑... 我认为您错过了对Web服务器的每个请求都是唯一的想法,因此您需要一种方法来在回发之间保持控制台程序的输出。下面,我使用Session。
背后的代码
protected void Timer1_Tick(object sender, EventArgs e)
{
Output_lbl.Text+=Session["Text"].ToString();
}
protected void OutputReceived(object sender, DataReceivedEventArgs e)
{
Session["Text"]+=e.Data;
System.Diagnostics.Debug.WriteLine(Text);
}
ASPX页面。
<asp:UpdatePanel runat="server" ID="UpdatePanel1" UpdateMode="Conditional" ChildrenAsTriggers="false">
<Triggers>
<asp:AsyncPostBackTrigger ControlID="Timer1" EventName="Tick" />
</Triggers>
<ContentTemplate>
<asp:Timer runat="server" ID="Timer1" OnTick="Timer1_Tick" Interval="150" />
<asp:Label ID="Output_lbl" runat="server" />
</ContentTemplate>
</asp:UpdatePanel>