ASP.NET更改服务器IP地址并重定向到登录页面

时间:2013-06-10 18:30:24

标签: c# asp.net

我在ASP.NET / C#中创建一个Web应用程序,允许用户远程管理小型Panel PC(服务器)。其中一个要求是为用户提供更改Panel PC上以太网端口的所有网络相关设置的能力。这将包括IP地址,子网掩码,默认网关和DNS服务器。因此,如果用户更改了很可能会终止其当前浏览器会话的设置(如IP地址或默认网关),我想显示一个对话框,让他们知道当前会话将(或已经)结束,然后重定向再次登录页面。

我尝试过使用AJAX ModalPopup,其中对话框的onClick事件OK按钮,执行必要的服务器端(C#)代码来更改实际的网络设置,OnClientClick属性设置为调用javascript函数进行重定向再次登录页面。重定向正在运行,但后面的onClick代码没有被调用。我一直在研究使用Page.ClientScript.RegisterStartupScript(),但我不确定这是否适用于这种情况,如果是这样,我的代码背后会去哪里?

我不是百分之百确定我尝试实现这种方式是最好的方式,或者甚至可以按照我想要的方式做到这一点。我愿意接受任何选择或建议。

这是我的代码:

<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;
        }
    }
}

1 个答案:

答案 0 :(得分: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;
}