Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / web / ASP.NET

Extending Session and Timed Alert Message in ASP.NET

4.33/5 (3 votes)
14 Apr 2011CPOL 19.2K  
This article about how to extend your ASP.NET session and alert the user based some predefined time

This article shows how to display an alert message and prompt the user whether he wants to extend the session. If the user clicks OK, then it will automatically increase the session, else it will close the session with a warning message.

You can also change and update the code based on your requirements, this is just sample to show how to extend the session and show a timed-out alert message.


HTML
<script language="javascript" type="text/javascript">
    AlertTimeOut = 0.25 * 60 * 1000; // Milliseconds, Alert Message       
        WarnTimeOut = 0.25 * 60 * 1000; //  Warn Message before 5 min of session out
        setTimeout("WarnUser();", WarnTimeOut);
        //setTimeout("AlertUser();", AlertTimeOut);
         function AlertUser()
        {
           alert('Your session has been expired, please save your data outside the system [copy the data in word document].');        
        }
        function WarnUser()
        {
            var currentTime = new Date()
            var hours = currentTime.getHours()
            var minutes = currentTime.getMinutes()
            if (minutes < 10){
            minutes = "0" + minutes
            }            
            if(hours > 11){
            var tempTime = hours + ":" + minutes + "PM"
            } else {
            var tempTime = hours + ":" + minutes + "AM"
            }
            var ans = confirm("Your login session is about to expire in 5 minutes.Do you want to extend it? - Date: " + tempTime);

            if (ans)
            {
                var oImage = new Image;
                oImage.src = "tempdummy.gif" + Math.random(); 
                setTimeout("WarnUser();", WarnTimeOut);                
            }
            else
            {
                 setTimeout("AlertUser();", AlertTimeOut);
            }
        }
        </script>

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)