0

I am creating a web application in ASP.NET/C# that allows users to remotely manage a small Panel PC (the server). One of the requirement is to provide the ability for the user to change all of the network related settings of the Ethernet port on the Panel PC. This would include IP Address, Sub Net Mask, Default Gateway and DNS servers. So if the user changes a setting (like the IP Address or Default Gateway) that will most likely terminate their current browser session, I want to display a dialog box letting them know that their current session will (or has) ended and then redirect them to the login page again.

I have tried using the AJAX ModalPopup, where the onClick event of the dialog box OK button, executes the necessary server side (C#) code to change the actual network settings and the OnClientClick property is set to call a javascript function to redirect to the login page again. The redirect is working but the onClick code behind isn't getting called. I've been looking into using Page.ClientScript.RegisterStartupScript() but I'm not sure whether that will work in this case and if so where in my code behind would it go?

I'm not 100% sure the way I'm trying to implement this is the best way or if it's even possible to do what I want this way. I'm open to any options or suggestions.

Here is my code:

<script type="text/javascript">
    function Logout () 
    {
        window.location.href = "Login.aspx";
    };
</script>


<asp:Label ID="dummyLabel" runat="server" />    
<asp:Panel ID="AlertBox" runat="server" Style="display: none;" CssClass="modalPopup">
    <div style="border: 3px solid #000000">
        <table style="background-color: #e3e2fe;">
            <tr>
                <td align="center">
                    <div id="divCEMsg" runat="server" style="text-align: left; width: 300px; margin-left:5px;">
                        <asp:Label ID="Label34" runat="server" Text="You are changing the IP Settings so your current web browser session will be disconnected.<br /><br />To log back in you will need to use the newly assigned IP Address<br /><br />Click OK to proceed with network setting changes"></asp:Label>
                    </div>
                    <br/><br/>
                </td>
            </tr>

            <tr>                
                <td align="center">
                    <asp:Button ID="btnOk" runat="server" Text="OK" OnClientClick="Logout();" onclick="btnOk_Click" ></asp:Button>
                </td>
            </tr>
        </table>
    </div>
</asp:Panel>
<ajaxToolkit:ModalPopupExtender ID="ModalPopupExtender1" runat="server" 
    PopupControlID="AlertBox" BackgroundCssClass="modalBackground" DropShadow="true" TargetControlID="dummyLabel">
</ajaxToolkit:ModalPopupExtender>


protected void btnOk_Click(object sender, EventArgs e)
{
    //save the settings to the database
    if (saveSetupDataToDatabase())
    {
        //update the Panel PC network settings
        if (!updatePanelPcNetworkSettings())
        {
            return;
        }
    }
}
4

1 回答 1

0

好吧,我想通了,并且能够让它工作。这是我最终得到的结果:

<script type="text/javascript">

    function RedirectToLoginPage()
    {
        // Get the new URL from our session variable
        var newUrl = '<%=Session["newURL"]%>'
        window.location.href = newUrl;//go to the new URL
    };


    function ChangeIpSettings(object)
    {
        //redirect user to login page after 3 seconds.  This should give the server enough time to change it's network settings
        var t = setTimeout(RedirectToLoginPage, 3000)

        //  disable the button just clicked so the user can't click it again
        document.getElementById('okButton').disabled = true;

        //call the "ChangePanelPcNetworkSettings()" method in the code behind.  This is the method that will actually change the Panel PC's network settings
        PageMethods.ChangePanelPcNetworkSettings(onSucceeded, onFailed);
    }

    function onSucceeded(result, userContext, methodName)
    {
    //do nothing
    }

    function onFailed(error, userContext, methodName)
    {
        //alert("An error occurred")
    }

</script>

<asp:Label ID="dummyLabel" runat="server" />
<asp:Panel ID="AlertBox" runat="server" Style="display: none;" CssClass="modalPopup">
    <div style="border: 3px solid #000000">
        <table style="background-color: #e3e2fe;">
            <tr>
                <td align="center">
                    <div id="divCEMsg" runat="server" style="text-align: left; width: 300px; margin-left:5px;">
                        <asp:Label ID="Label34" runat="server" Text="You are changing the IP Settings so your current web browser session will be disconnected.<br /><br />To log back in you will need to use the newly assigned IP Address<br /><br />Click OK to proceed with network setting changes"></asp:Label>
                    </div>
                    <br/><br/>
                </td>
            </tr>

            <tr>
                <td align="center">
                    <input id='okButton' type='button' value='OK' onclick='return ChangeIpSettings(this);' />
                </td>
            </tr>
        </table>
    </div>
</asp:Panel>
<ajaxToolkit:ModalPopupExtender ID="ModalPopupExtender1" runat="server"
    PopupControlID="AlertBox" BackgroundCssClass="modalBackground" DropShadow="true" TargetControlID="dummyLabel">
</ajaxToolkit:ModalPopupExtender>


[System.Web.Services.WebMethod]
public static bool ChangePanelPcNetworkSettings()
{
    //update the Panel PC network settings with IP settings entered on the web page
    if (!updatePanelPcNetworkSettings(webpageSystemSettings))
    {
        return false;
    }

    return true;
}    
于 2013-06-21T20:49:47.160 回答