在某些查詢頁面,如果是用Session存放資料表,當發生timeout時,往往必須重新執行查詢操作。為了使操作者有更好的使用體驗,在某些重要頁面可以加入讓Session 不要發生timeout。

如果Session timeout預設是20分鐘,可以在頁面增加計時器,每隔一分鐘減少儲存的變數一次,當變數剩下一分鐘,代表即將發生timeout,此時就利用post方式重新計時。

 

[前端]

<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true">

<script type="text/javascript">
            var sessionTimeout = <%= Session.Timeout %>

                function keepSession() {
                    document.getElementById("<%= Label2.ClientID %>").innerText = sessionTimeout;
                    sessionTimeout = sessionTimeout - 1;
               if (sessionTimeout > 1) { 
                    window.setTimeout("keepSession()", 60000);
                }
               else{
               $.post("session_keep.ashx");
                 sessionTimeout = <%= Session.Timeout %>; 
                 window.setTimeout("keepSession()", 60000);
              }
          }

</script>

 

[後端]

if (!IsPostBack) {

Page.ClientScript.RegisterStartupScript(this.GetType(), "onLoad", "keepSession();",true);

}

arrow
arrow
    全站熱搜

    門外漢 發表在 痞客邦 留言(0) 人氣()