2012-09-05 39 views
1

所以,我有一箇中繼器,顯示遠程服務器上的自定義函數列表。中繼器顯示如下。使用ServerController和更新面板更新不起作用

serverName serviceName serviceStatus Button1 Button2 
---------------------------------------------------------- 
Carolina StatsTracker  Running  update stop 
... 
.. 
. 

的serviceStatus被振打在一個更新面板中繼

<asp:UpdatePanel ID="statusUpdate" runat="server" UpdateMode="Conditional" ChildrenAsTriggers="false"> 
       <ContentTemplate>       
        <td><asp:Label ID="RowStatus" Width="100px" Text="Status..." runat="server" /></td> 
       </ContentTemplate> 
      </asp:UpdatePanel> 

內部和在後面的代碼我經由servercontroller和試圖更新serviceStatus實時或至少爲向服務器發送命令儘可能接近實時。像這樣

   if (svc.Status != ServiceControllerStatus.Stopped) 
       { 
        svc.Stop(); 
        StatusLabel.Text = "Stopping"; 
        statusUPdatePanel.Update(); 

        while (svc.Status != ServiceControllerStatus.Stopped) 
        { 
         System.Threading.Thread.Sleep(1000); 
         svc.Refresh(); 
        } 
        StatusLabel.Text = svc.Status.ToString(); 
        statusUPdatePanel.Update(); 

       } 
       System.Threading.Thread.Sleep(1000); 
       if (svc.Status != ServiceControllerStatus.Running) 
       { 
        svc.Start(); 
        StatusLabel.Text = "Starting"; 
        statusUPdatePanel.Update(); 

        while (svc.Status != ServiceControllerStatus.Running) 
        { 
         System.Threading.Thread.Sleep(1000); 
         svc.Refresh(); 
        } 
        StatusLabel.Text = svc.Status.ToString(); 
        statusUPdatePanel.Update(); 

       } 

此問題是狀態不會實時更新。它只會更新爲正在運行或錯誤的最終值。但從不顯示停止,開始或停止。還在按鈕上單擊我禁用按鈕時,serverController正在運行,一個小型用戶打樣步驟,並且似乎不會導致按鈕被禁用。我讀過大量的updatepanel問題帖子,認爲這可能是一個有約束力的問題,但我不確定,因爲我已經厭倦了許多建議的解決方案,無濟於事。
在此先感謝您的幫助。

回答

1

根據你的問題你想刷新你的updatePanel 兩次!這是不可能的。 (部分)回發只會發生在您的方法完成...此外,那些while (...)是一個壞主意,imo。您可能使用

ServiceController.WaitForStatus(ServiceControllerStatus.Running, new TimeSpan(0, 0, 5)); 

但不會照顧你的問題/問題,你想看到這兩個狀態(xxxPending和XXX)。我認爲這隻有一次機會 - 您必須重新詢問您的服務器以瞭解給定服務的當前狀態。所以,我創建了一個例子,其中

  1. 我使用jQuery的AJAX調用服務
  2. 改變的狀態上成功採取立即服務器響應秀它在客戶端(通常爲xxxPending
  3. 等待5秒在客戶端和S上重新向服務器請求該服務

工作得很好,我的現狀 - 與本地服務進行了測試。

serviceController_Repeater.aspx

<head runat="server"> 
    <title></title> 
    <script type="text/javascript" src="js/jquery-1.7.1.js"></script> 
    <script type="text/javascript"> 
     $(document).ready(function() { 
      console.log("Add AJAX calls to buttons\n"); 
      $('input[type=button]').click(
        function() { 
         changeStatus($(this).attr("value"), $(this).parents("tr").find("span[id$=lblName]"), $(this).parents("tr").find("span[id$=lblStatus]")); 
        }); 
      }); 

      function changeStatus(newStatus, displayLabel, statusLabel) { 
       var displayName = displayLabel.text();  
       console.log("change status to '" + newStatus + "' for service '" + displayName + "'"); 
       $.ajax({ 
        type: "POST", 
        url: "serviceController_Repeater.aspx/ChangeStatus", 
        data: "{ 'newStatus': '" + newStatus + "', 'displayName' : '" + displayName + "'}", 
        contentType: "application/json; charset=utf-8", 
        dataType: "json", 
        success: function (msg) { 
         console.log("server says\n\tnew status is now: " + msg.d); 
         statusLabel.text(msg.d); 
         if (newStatus != "") 
          window.setTimeout(function() { changeStatus("", displayLabel, statusLabel) }, 5000); 
        } 
       }); 
      } 
    </script> 
</head> 
<body> 
    <form id="form1" runat="server"> 
    <div> 
     <asp:Repeater ID="rptServices" runat="server"> 
      <HeaderTemplate> 
       <table> 
        <thead> 
         <tr> 
          <th> 
           Name 
          </th> 
          <th> 
           Status 
          </th> 
          <th colspan="4"> 
          </th> 
         </tr> 
        </thead> 
        <tbody> 
      </HeaderTemplate> 
      <FooterTemplate> 
       </tbody></table></FooterTemplate> 
      <ItemTemplate> 
       <tr id="1"> 
        <td> 
         <asp:Label ID="lblName" runat="server" Text='<%# Eval("DisplayName") %>'></asp:Label> 
        </td> 
        <td> 
         <asp:Label ID="lblStatus" runat="server" Text='<%# Eval("Status") %>'></asp:Label> 
        </td> 
        <td> 
         <input type="button" <%# Eval("Status").ToString()=="Stopped" ? "" : "disabled" %> value="Start" /> 
        </td> 
        <td> 
         <input type="button" <%# Eval("Status").ToString()=="Running" ? "" : "disabled" %> value="Stop" /> 
        </td> 
        <td> 
         <input type="button" <%# Eval("Status").ToString()=="Running" ? "" : "disabled" %> value="Pause" /> 
        </td> 
        <td> 
         <input type="button" <%# Eval("Status").ToString()=="Paused" ? "" : "disabled" %> value="Continue" /> 
        </td> 
       </tr> 
      </ItemTemplate> 
     </asp:Repeater> 
    </div> 
    </form> 
</body> 

serviceController_Repeater.aspx。cs

protected void Page_Load(object sender, EventArgs e) 
{ 
    if (!Page.IsPostBack) 
     bindServices(); 
} 
private void bindServices() 
{ 
    ServiceController[] services = ServiceController.GetServices(); 
    rptServices.DataSource = services; 
    rptServices.DataBind(); 
} 

[WebMethod] 
public static string ChangeStatus(string newStatus, string displayName)// 
{ 
    Debug.WriteLine(string.Format("Service {0} new status {1}", displayName, newStatus)); 
    ServiceController sc = new ServiceController(displayName); 

    switch (newStatus) 
    { 
     case "Start": 
      sc.Start(); 
      // instead of waiting on the SERVER for xxx secondes 
      // immediately RETURN to CLIENT 
      //sc.WaitForStatus(ServiceControllerStatus.Running, new TimeSpan(0, 0, 15)); 
      break; 
     case "Stop": 
      sc.Stop(); 
      break; 
     case "Pause": 
      sc.Pause(); 
      break; 
     case "Continue": 
      sc.Continue(); 
      break; 
     default: 
      break; 
    } 
    return sc.Status.ToString(); 
} 
+0

感謝您的回覆,這似乎相當有用。特別是'ServiceController.WaitForStatus(ServiceControllerStatus.Running,新的TimeSpan(0,0,5));'但我想更多地讓更新面板更新多次文本更改事件,但不確定。也許可以通過與代碼後面的repeater item命令事件相互依賴的另一種方法。不過,我相信你的回答是有效的,但是會等到我能實施它,看看它是否適用於我的情況,作爲答案。正如我所指出的那樣,非常感謝 – user1250570

+0

,在一個請求中有**無法從_server更新面板多次**。你將不得不提出兩個請求(_partial,完整 - 通過按鈕或ajax ..._) - 很高興我可以幫助 –

+0

所以我有一個問題適應這個主頁面,我不是很熟悉jQuery的。我假設我需要調整標識符? – user1250570