Click here to Skip to main content
Click here to Skip to main content

Prevent Session Timeout in ASP.NET

, 16 Feb 2005
Rate this:
Please Sign up or sign in to vote.
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 Wink | ;-) 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
My Evolution:
TRS-80 Basic, Clipper, C, Better Basic, FORTRAN, C++, Visual Basic, Delphi, C#

Comments and Discussions

 
GeneralThere is a super simple way to handle this, about 8 lines of code and an iframe PinmemberPatrick_SURFThru18-May-11 10:03 
Generallogin.aspx PinmemberAjay Kale New27-Sep-10 0:16 
Generalsession end PinmemberAjay Kale New9-Sep-10 4:17 
GeneralMy vote of 5 Pinmembernavneelb3-Aug-10 3:40 
QuestionNot working when run using IIS Pinmembershyaz24-Jun-10 11:07 
GeneralIt can be done in multiple ways PinmemberEaverae1-Sep-09 22:31 
GeneralGood Article... This is save my day... Pinmemberyogi230-Apr-09 17:10 
Questionstop online banking time out Pinmemberjsmith0761124-Jan-09 22:14 
AnswerRe: stop online banking time out Pinmembermetweek5-Mar-09 17:28 
GeneralASP.NET 2.0 Update w/AJAX Support PinmemberTim McCurdy28-Dec-08 14:26 
GeneralRe: ASP.NET 2.0 Update w/AJAX Support PinmemberLongTom891-May-09 5:28 
GeneralRe: ASP.NET 2.0 Update w/AJAX Support PinmemberTim McCurdy1-May-09 10:57 
GeneralRe: ASP.NET 2.0 Update w/AJAX Support PinmemberJohn Gleeson4-May-09 23:59 
QuestionRe: ASP.NET 2.0 Update w/AJAX Support Pinmemberthomasabcd2-Jun-09 0:12 
AnswerRe: ASP.NET 2.0 Update w/AJAX Support PinmemberTim McCurdy2-Jun-09 2:00 
GeneralRe: ASP.NET 2.0 Update w/AJAX Support Pinmemberthomasabcd2-Jun-09 4:13 
GeneralRe: ASP.NET 2.0 Update w/AJAX Support Pinmemberkoger13-Sep-10 22:58 
GeneralRe: ASP.NET 2.0 Update w/AJAX Support PinmemberTim McCurdy14-Sep-10 2:57 
GeneralDisable caching Pinmemberrtitulaer2-Oct-08 5:33 
QuestionWhat about using an ajax call? PinmemberStuart Campbell13-Apr-08 18:54 
AnswerRe: What about using an ajax call? PinmemberJitu9914-Apr-08 6:41 
GeneralRe: What about using an ajax call? PinmemberJorge Bay Gondra19-Sep-08 0:47 
GeneralRe: What about using an ajax call? PinmemberMember 307583727-Mar-09 14:02 
AnswerRe: What about using an ajax call? Pinmemberjohnlysam16-Jul-09 9:52 
GeneralPlease Help Pinmembershetty7714-Jan-08 4:06 

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.

| Advertise | Privacy | Mobile
Web04 | 2.8.140721.1 | Last Updated 16 Feb 2005
Article Copyright 2005 by Ach1lles
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid