简体   繁体   English

使用ServerController和更新面板更新不起作用

[英]Using ServerController and Update Panel update not working

So, I have a repeater that displays a list of custom functions on a remote server. 因此,我有一个中继器,它在远程服务器上显示自定义功能的列表。 The repeater displays as follows. 中继器显示如下。

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

The serviceStatus is rapped in an update panel inside the repeater 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>

and in the code behind I send commands to the server via servercontroller and attempt to update the serviceStatus real time or at least as close as possible to real time. 并在后面的代码中,我通过servercontroller向服务器发送命令,并尝试实时更新serviceStatus或至少实时更新。 like this 像这样

                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();

                } 

This issue is that the status doesn't update real time. 问题是状态不会实时更新。 It only updates to the final value which is either running or error. 它只会更新为正在运行或错误的最终值。 but never shows the stopping, starting, or stopped as it happens. 但绝不会显示正在发生的停止,开始或停止。 also on the button click I disable the buttons while the serverController is running, a small user proofing step and which doesn't seem to cause the buttons to become disabled. 还要在按钮上单击,在serverController运行时禁用按钮,这是一个很小的用户验证步骤,似乎并没有导致按钮被禁用。 I've read threw a large number of updatepanel issue post and think it might be a binding issue but i'm am not sure because i have tired the many of those suggested solutions to no avail. 我已经阅读了很多有关updatepanel的文章,并认为这可能是一个具有约束力的问题,但是我不确定,因为我已经厌倦了许多建议的解决方案,但无济于事。
Thanks in advance for any help. 在此先感谢您的帮助。

According to your question you would like to refresh your updatePanel twice ! 根据您的问题,您想刷新 两次 updatePanel! That is not possible. 这是不可能的。 The (partial) postback will only occur on your method completing... Furthermore those while (...) are a bad idea, imo. (部分)回发将仅在您的方法完成时发生...此外,那些while (...)是个坏主意,imo。 You could rather use 可以 使用 ,而

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

But that would not take care of your problem/question that you would like to see BOTH status (xxxPending and xxx). 但这并不能解决您希望同时看到两个状态(xxxPending和xxx)的问题。 I think there is only one chance to this - you have to re-ask your server for the current status of a given service. 我认为这样做只有一次机会-您必须向服务器重新询问给定服务的当前状态。 So I created an example in which 所以我创建了一个例子

  1. I use an JQUery AJAX call to change the status of a service 我使用JQUery AJAX调用更改服务状态
  2. on success I take the immediate server response and show it on the client (usually xxxPending ) 成功眼前的服务器响应 ,并显示在客户端 (通常xxxPending)
  3. wait for 5 secs on the client and re-ask the server for the current status of this service 在客户端上等待5秒钟 ,然后向服务器重新询问该服务的当前状态

Worked fine for me - tested it with my local services. 对我来说效果很好-已通过我的本地服务进行了测试。

serviceController_Repeater.aspx 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 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();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM