show出Sesson Timeout還剩多少時間

JavaScrip抓Session TimeOut時間,並每分鐘秀在網頁上,過時則跳出警告視窗

 

 

JavaScrip抓Session TimeOut時間,並每分鐘秀在網頁上,過時則跳出警告視窗

<%@ Page Language="VB" AutoEventWireup="true" CodeFile="Default.aspx.vb" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Untitled Page</title>

    <script type="text/javascript">
        var sessionTimeout = <%= Session.Timeout %>
        
        function DisplaySessionTimeout()
        {
            document.getElementById("<%= lblSessionTime.ClientID %>").innerText = sessionTimeout;
            sessionTimeout = sessionTimeout - 1;
            
            if (sessionTimeout >= 0)
                window.setTimeout("DisplaySessionTimeout()", 60000);
            else
            {
                alert("等待時間過久,請重新登入網頁");
            }
        }
    </script>

</head>
<body>
    <form id="form1" runat="server">
        <asp:ScriptManager ID="ScriptManager1" runat="server" />
        <div>
            網頁剩餘session timeout 時間:還有<asp:Label ID="lblSessionTime" runat="server" Text=""></asp:Label>分鐘
        </div>
    </form>
</body>
</html>

 

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not Me.IsPostBack Then
            Page.ClientScript.RegisterStartupScript(Me.[GetType](), "onLoad", "DisplaySessionTimeout();", True)
        End If
        
    End Sub

參考至:http://www.codeproject.com/KB/scripting/Session_Timeout.aspx