Skip to main content
Email Password   helpLost your password?

Introduction

I have developed ASP and ASP.NET sites for many years and one of the most common end user problems (apart from basic stupidity ;-) is that while the user is entering information into a web form or HTML edit box, the session timeout period will elapse and they lose all the work they have done.

I have tried solutions such as making JavaScript alert the user to click a button or refresh page, but this has restrictions, especially if they are not able to submit the form yet due to required field limitations.

Solution

I recently came across some code which attempted to fix this problem but that was unsuccessful because the author had forgotten the issue of client side caching.

Add to your page the following code:

private void Page_Load(object sender, System.EventArgs e)
{
this.AddKeepAlive();
}
private void AddKeepAlive()
{
int int_MilliSecondsTimeOut = (this.Session.Timeout * 60000) - 30000;
string str_Script = @"
<script type='text/javascript'>
//Number of Reconnects
var count=0;
//Maximum reconnects setting
var max = 5;
function Reconnect(){

count++;
if (count < max)
{
window.status = 'Link to Server Refreshed ' + count.toString()+' time(s)' ;

var img = new Image(1,1);

img.src = 'Reconnect.aspx';

}
}

window.setInterval('Reconnect()',"+ _
    int_MilliSecondsTimeOut.ToString()+ @"); //Set to length required

</script>

";

this.Page.RegisterClientScriptBlock("Reconnect", str_Script);

}

This code will cause the client to request within 30 seconds of the session timeout the page Reconnect.aspx.

The Important Part

Now this works the first time but if the page is cached locally then the request is not made to the server and the session times out again, so what we have to do is specify that the page Reconnect.aspx cannot be cached at the server or client.

This is done by creating the Reconnect.aspx page with this content:

<%@ OutputCache Location="None" VaryByParam="None" %>
<html>
</html>

The OutputCache directive prevents this from being cached and so the request is made each time. No code behind file will be needed for this page so no @Page directive is needed either.

And that's it.

Hope this helps someone.

You must Sign In to use this message board.
 
 
Per page   
 FirstPrevNext
GeneralIt can be done in multiple ways Pin
Eaverae
23:31 1 Sep '09  
GeneralGood Article... This is save my day... Pin
yogi2
18:10 30 Apr '09  
Questionstop online banking time out Pin
jsmith07611
23:14 24 Jan '09  
AnswerRe: stop online banking time out Pin
metweek
18:28 5 Mar '09  
GeneralASP.NET 2.0 Update w/AJAX Support Pin
Tim McCurdy
15:26 28 Dec '08  
GeneralRe: ASP.NET 2.0 Update w/AJAX Support Pin
LongTom89
6:28 1 May '09  
GeneralRe: ASP.NET 2.0 Update w/AJAX Support Pin
Tim McCurdy
11:57 1 May '09  
GeneralRe: ASP.NET 2.0 Update w/AJAX Support Pin
John Gleeson
0:59 5 May '09  
QuestionRe: ASP.NET 2.0 Update w/AJAX Support Pin
thomasabcd
1:12 2 Jun '09  
AnswerRe: ASP.NET 2.0 Update w/AJAX Support Pin
Tim McCurdy
3:00 2 Jun '09  
GeneralRe: ASP.NET 2.0 Update w/AJAX Support Pin
thomasabcd
5:13 2 Jun '09  
GeneralDisable caching Pin
rtitulaer
6:33 2 Oct '08  
GeneralWhat about using an ajax call? Pin
Stuart Campbell
19:54 13 Apr '08  
GeneralRe: What about using an ajax call? Pin
Jitu99
7:41 14 Apr '08  
GeneralRe: What about using an ajax call? Pin
Jorge Bay Gondra
1:47 19 Sep '08  
GeneralRe: What about using an ajax call? Pin
Member 3075837
15:02 27 Mar '09  
GeneralRe: What about using an ajax call? Pin
johnlysam
10:52 16 Jul '09  
GeneralPlease Help Pin
shetty77
5:06 14 Jan '08  
GeneralRe: Please Help Pin
Ach1lles
5:38 14 Jan '08  
GeneralRe: Please Help Pin
shetty77
5:27 15 Jan '08  
GeneralThanks much, but what is redirect.aspx? Pin
Garry Freemyer
13:17 30 Nov '07  
GeneralIt don't work for me. I'm stuck, They are demanding this! Pin
Garry Freemyer
14:35 3 Dec '07  
GeneralRe: It don't work for me. I'm stuck, They are demanding this! Pin
Garry Freemyer
12:09 7 Dec '07  
QuestionPlease Help Pin
Dubey Yogendra
5:42 16 Oct '07  
AnswerRe: Please Help Pin
Ach1lles
7:18 18 Oct '07  


Last Updated 16 Feb 2005 | Advertise | Privacy | Terms of Use | Copyright © CodeProject, 1999-2009