Click here to Skip to main content
6,595,854 members and growing! (18,032 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#, Windows, .NET, ASP.NET, IIS, Visual Studio, IE 6.0, IE 5.5, Dev
Posted:16 Feb 2005
Views:267,825
Bookmarked:113 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
47 votes for this article.
Popularity: 7.24 Rating: 4.33 out of 5
2 votes, 4.3%
1
1 vote, 2.2%
2
3 votes, 6.5%
3
13 votes, 28.3%
4
27 votes, 58.7%
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


Member
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
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 25 of 101 (Total in Forum: 101) (Refresh)FirstPrevNext
GeneralIt can be done in multiple ways PinmemberEaverae23:31 1 Sep '09  
GeneralGood Article... This is save my day... Pinmemberyogi218:10 30 Apr '09  
Questionstop online banking time out Pinmemberjsmith0761123:14 24 Jan '09  
AnswerRe: stop online banking time out Pinmembermetweek18:28 5 Mar '09  
GeneralASP.NET 2.0 Update w/AJAX Support PinmemberTim McCurdy15:26 28 Dec '08  
GeneralRe: ASP.NET 2.0 Update w/AJAX Support PinmemberLongTom896:28 1 May '09  
GeneralRe: ASP.NET 2.0 Update w/AJAX Support PinmemberTim McCurdy11:57 1 May '09  
GeneralRe: ASP.NET 2.0 Update w/AJAX Support PinmemberJohn Gleeson0:59 5 May '09  
QuestionRe: ASP.NET 2.0 Update w/AJAX Support Pinmemberthomasabcd1:12 2 Jun '09  
AnswerRe: ASP.NET 2.0 Update w/AJAX Support PinmemberTim McCurdy3:00 2 Jun '09  
GeneralRe: ASP.NET 2.0 Update w/AJAX Support Pinmemberthomasabcd5:13 2 Jun '09  
GeneralDisable caching Pinmemberrtitulaer6:33 2 Oct '08  
GeneralWhat about using an ajax call? PinmemberStuart Campbell19:54 13 Apr '08  
GeneralRe: What about using an ajax call? PinmemberJitu997:41 14 Apr '08  
GeneralRe: What about using an ajax call? PinmemberJorge Bay Gondra1:47 19 Sep '08  
GeneralRe: What about using an ajax call? PinmemberMember 307583715:02 27 Mar '09  
GeneralRe: What about using an ajax call? Pinmemberjohnlysam10:52 16 Jul '09  
GeneralPlease Help Pinmembershetty775:06 14 Jan '08  
GeneralRe: Please Help PinmemberAch1lles5:38 14 Jan '08  
GeneralRe: Please Help Pinmembershetty775:27 15 Jan '08  
GeneralThanks much, but what is redirect.aspx? PinmemberGarry Freemyer13:17 30 Nov '07  
GeneralIt don't work for me. I'm stuck, They are demanding this! PinmemberGarry Freemyer14:35 3 Dec '07  
GeneralRe: It don't work for me. I'm stuck, They are demanding this! PinmemberGarry Freemyer12:09 7 Dec '07  
QuestionPlease Help PinmemberDubey Yogendra5:42 16 Oct '07  
AnswerRe: Please Help PinmemberAch1lles7:18 18 Oct '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
Web22 | Advertise on the Code Project