2014-01-07 26 views
0

我試圖顯示從我的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; 
} 

回答

2

你的面板不會更新,因爲你將UpdateMode設置爲條件,而不指定會導致它異步回發到服務器並獲取更新的內容有任何觸發器。您需要在UpdatePanel中嵌入一個Timer,並將其配置爲UpdatePanel的觸發器。看文章How to refresh update panel with a timer.

而不是使用更新面板,你爲什麼不嘗試使用AJAX結合web方法? UpdatePanels非常難以使用。

編輯... 我認爲你錯過了對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> 
+0

我編輯了我的問題。另外,我不確定如何去做你建議的AJAX。我需要在按鈕按下時運行批處理文件(在服務器上),然後將控制檯的輸出流式傳輸到網頁。 – kschieck

+0

@kschieck我編輯了我的答案。 – mason

+0

謝謝它完美的作品 – kschieck

相關問題