Click here to Skip to main content
Licence 
First Posted 16 Feb 2005
Views 406,993
Bookmarked 132 times

Prevent Session Timeout in ASP.NET

By | 16 Feb 2005 | Article
Simple code to prevent a sesison from timing out while a user enters data or edits HTML etc.

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 = '/KB/session/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.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Ach1lles

Founder
Tech Dept
United Kingdom United Kingdom

Member

My Evolution:
TRS-80 Basic, Clipper, C, Better Basic, FORTRAN, C++, Visual Basic, Delphi, C#


Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralThere is a super simple way to handle this, about 8 lines of code and an iframe PinmemberPatrick_SURFThru10:03 18 May '11  
Generallogin.aspx PinmemberAjay Kale New0:16 27 Sep '10  
Generalsession end PinmemberAjay Kale New4:17 9 Sep '10  
GeneralMy vote of 5 Pinmembernavneelb3:40 3 Aug '10  
QuestionNot working when run using IIS Pinmembershyaz11:07 24 Jun '10  
GeneralIt can be done in multiple ways PinmemberEaverae22:31 1 Sep '09  
GeneralGood Article... This is save my day... Pinmemberyogi217:10 30 Apr '09  
Questionstop online banking time out Pinmemberjsmith0761122:14 24 Jan '09  
AnswerRe: stop online banking time out Pinmembermetweek17:28 5 Mar '09  
GeneralASP.NET 2.0 Update w/AJAX Support PinmemberTim McCurdy14:26 28 Dec '08  
GeneralRe: ASP.NET 2.0 Update w/AJAX Support PinmemberLongTom895:28 1 May '09  
GeneralRe: ASP.NET 2.0 Update w/AJAX Support PinmemberTim McCurdy10:57 1 May '09  
GeneralRe: ASP.NET 2.0 Update w/AJAX Support PinmemberJohn Gleeson23:59 4 May '09  
QuestionRe: ASP.NET 2.0 Update w/AJAX Support Pinmemberthomasabcd0:12 2 Jun '09  
AnswerRe: ASP.NET 2.0 Update w/AJAX Support PinmemberTim McCurdy2:00 2 Jun '09  
GeneralRe: ASP.NET 2.0 Update w/AJAX Support Pinmemberthomasabcd4:13 2 Jun '09  
GeneralRe: ASP.NET 2.0 Update w/AJAX Support Pinmemberkoger22:58 13 Sep '10  
GeneralRe: ASP.NET 2.0 Update w/AJAX Support PinmemberTim McCurdy2:57 14 Sep '10  
GeneralDisable caching Pinmemberrtitulaer5:33 2 Oct '08  
QuestionWhat about using an ajax call? PinmemberStuart Campbell18:54 13 Apr '08  
AnswerRe: What about using an ajax call? PinmemberJitu996:41 14 Apr '08  
How to implement using ajax call?
thank you for considering.
GeneralRe: What about using an ajax call? PinmemberJorge Bay Gondra0:47 19 Sep '08  
GeneralRe: What about using an ajax call? PinmemberMember 307583714:02 27 Mar '09  
AnswerRe: What about using an ajax call? Pinmemberjohnlysam9:52 16 Jul '09  
GeneralPlease Help Pinmembershetty774:06 14 Jan '08  

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

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

Permalink | Advertise | Privacy | Mobile
Web02 | 2.5.120529.1 | Last Updated 16 Feb 2005
Article Copyright 2005 by Ach1lles
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid