2012-09-05 44 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