23 May 2010

Show Session Timeout countdown on ASP.NET page

Hi,

I came across a really nice feature that you can use in ASP.NET to show session timeout to users. This provides rich user experience. I used javascript to create this facility. Hope this will help someone looking for the solution.

 var timeout = '<%= Session.Timeout * 60 * 1000 %>';  
 var timer = setInterval(function() { timeout -= 1000; document.getElementById('countDown').innerHTML = time(timeout); if (timeout == 0) { clearInterval(timer); alert('Your session has expired!') } }, 1000);  
 function two(x) { return ((x > 9) ? "" : "0") + x }  
 function time(ms) {  
 var t = '';  
 var sec = Math.floor(ms / 1000);  
 ms = ms % 1000  
 var min = Math.floor(sec / 60);  
 sec = sec % 60;  
 t = two(sec);  
 var hr = Math.floor(min / 60);  
 min = min % 60;  
 t = hr+":"+two(min) + ":" + t;  
 return "You session will timeout in " + t ;  
 }  


add a span named countDown to the page where you want to display this session timeout message. If you want it to display on all the pages once user is logged in then put it in Master page and voila !!! Your session timeout message will be there to warn user.

 <span id="countDown">  
  </span>  

No comments:

Post a Comment