Click here to Skip to main content
15,885,914 members
Articles / Web Development / ASP.NET
Tip/Trick

Extending Session and Timed Alert Message in ASP.NET

Rate me:
Please Sign up or sign in to vote.
4.33/5 (3 votes)
13 Apr 2011CPOL 19K   8   1
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)


Written By
Software Developer (Senior) Al Tayer Group
United Arab Emirates United Arab Emirates
This member doesn't quite have enough reputation to be able to display their biography and homepage.

Comments and Discussions

 
Questionimage required?? Pin
Member 1104661429-Aug-14 19:51
Member 1104661429-Aug-14 19:51 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.