ASP.NET - Javascript timeOut Warning based on sessionState timeOut in web.config

后端 未结 6 1609
囚心锁ツ
囚心锁ツ 2020-12-06 08:33

Problem: I am looking to create a time-out warning message on an asp.net page with a c# code behind based off my webconfig sessionState TimeOut Attribute.

Code on w

6条回答
  •  独厮守ぢ
    2020-12-06 09:20

    Pseudo code:

    • Read the timeout setting in codebehind
    • Register a ClientScriptblock (setTimeout passing the timeout period (20 * 60))
    • On timeout, display a warning label on the page

    Sample code:

        public void RegisterTimeoutWarning(System.Web.UI.Page Page)
        {
            var timeout = HttpContext.Current.Session.Timeout * 60 * 1000;
            Page.ClientScript.RegisterStartupScript(Page.GetType(), 
                    "timeoutWarning", 
                    string.Format("setTimeout(function () {{ alert('Session about to expire'); }}, {0});", timeout), true);
        }
    

    Of course, you can improve the client side display (rather than showing an alert) by displaying warning popups or even a confirm popup which you can then use to renew the session.

提交回复
热议问题