我試圖顯示從我的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;
}
我編輯了我的問題。另外,我不確定如何去做你建議的AJAX。我需要在按鈕按下時運行批處理文件(在服務器上),然後將控制檯的輸出流式傳輸到網頁。 – kschieck
@kschieck我編輯了我的答案。 – mason
謝謝它完美的作品 – kschieck