Click here to Skip to main content
5,788,212 members and growing! (19,082 online)
Email Password   helpLost your password?
Web Development » Session State » Sessions and Session State     Intermediate

Prevent Session Timeout in ASP.NET

By Ach1lles

Simple code to prevent a sesison from timing out while a user enters data or edits HTML etc.
C#, HTML, Windows, .NET, IIS, Visual Studio, ASP.NET, IE 6.0, IE 5.5, IE, Dev

Posted: 16 Feb 2005
Updated: 16 Feb 2005
Views: 204,000
Bookmarked: 94 times
Announcements
Loading...



Search    
Advanced Search
Sitemap
41 votes for this Article.
Popularity: 6.93 Rating: 4.30 out of 5
2 votes, 5.0%
1
1 vote, 2.5%
2
3 votes, 7.5%
3
10 votes, 25.0%
4
24 votes, 60.0%
5

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.

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


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

Occupation: Founder
Company: Tech Dept
Location: United Kingdom United Kingdom

Other popular Session State articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 25 of 87 (Total in Forum: 87) (Refresh)FirstPrevNext
GeneralASP.NET 2.0 Update w/AJAX SupportmemberTim McCurdy15:26 28 Dec '08  
GeneralDisable cachingmemberrtitulaer6:33 2 Oct '08  
GeneralWhat about using an ajax call?memberStuart Campbell19:54 13 Apr '08  
GeneralRe: What about using an ajax call?memberJitu997:41 14 Apr '08  
GeneralRe: What about using an ajax call?memberJorge Bay Gondra1:47 19 Sep '08  
GeneralPlease Helpmembershetty775:06 14 Jan '08  
GeneralRe: Please HelpmemberAch1lles5:38 14 Jan '08  
GeneralRe: Please Helpmembershetty775:27 15 Jan '08  
GeneralThanks much, but what is redirect.aspx?memberGarry Freemyer13:17 30 Nov '07  
GeneralIt don't work for me. I'm stuck, They are demanding this!memberGarry Freemyer14:35 3 Dec '07  
GeneralRe: It don't work for me. I'm stuck, They are demanding this!memberGarry Freemyer12:09 7 Dec '07  
QuestionPlease HelpmemberDubey Yogendra5:42 16 Oct '07  
AnswerRe: Please HelpmemberAch1lles7:18 18 Oct '07  
GeneralThis is an alternativememberswedishspeeder8:01 2 Sep '07  
GeneralRe: This is an alternativememberfa8011119:19 26 May '08  
GeneralRe: This is an alternativememberrtitulaer6:29 2 Oct '08  
GeneralThanksmemberCaptain CAD9:51 29 Aug '07  
GeneralMessage Automatically RemovedmemberAlireza Asgari7:58 6 Aug '07  
AnswerRe: your solution so stupidmemberyogi67813:40 6 Aug '07  
GeneralRe: your solution so stupidmemberDewey12:26 12 Aug '07  
GeneralCoolmemberHannes Foulds22:42 22 Jul '07  
GeneralThank you!memberbpdski6:07 20 Jul '07  
GeneralRe: Thank you!memberJigs Shah21:43 14 Dec '08  
GeneralPrevent Session Timeoutmemberkhanyile0:42 12 Jul '07  
GeneralRe: Prevent Session TimeoutmemberAch1lles3:07 12 Jul '07  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 16 Feb 2005
Editor: Rinish Biju
Copyright 2005 by Ach1lles
Everything else Copyright © CodeProject, 1999-2009
Web17 | Advertise on the Code Project