Click here to Skip to main content
15,114,784 members
Articles / Web Development / HTML
Posted 16 Feb 2005


144 bookmarked

Prevent Session Timeout in ASP.NET

Rate me:
Please Sign up or sign in to vote.
4.47/5 (57 votes)
16 Feb 20051 min read
Simple code to prevent a sesison from timing out while a user enters data or edits HTML etc.


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.


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)
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(){

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



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" %>

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.


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

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 Pin
MTRIG18-May-11 11:03
MemberMTRIG18-May-11 11:03 
Generallogin.aspx Pin
Ajay Kale New27-Sep-10 1:16
MemberAjay Kale New27-Sep-10 1:16 
Generalsession end Pin
Ajay Kale New9-Sep-10 5:17
MemberAjay Kale New9-Sep-10 5:17 
GeneralMy vote of 5 Pin
navneelb3-Aug-10 4:40
Membernavneelb3-Aug-10 4:40 
QuestionNot working when run using IIS Pin
shyaz24-Jun-10 12:07
Membershyaz24-Jun-10 12:07 
GeneralIt can be done in multiple ways Pin
Eaverae1-Sep-09 23:31
MemberEaverae1-Sep-09 23:31 
GeneralGood Article... This is save my day... Pin
yogi230-Apr-09 18:10
Memberyogi230-Apr-09 18:10 
Questionstop online banking time out Pin
jsmith0761124-Jan-09 23:14
Memberjsmith0761124-Jan-09 23:14 
AnswerRe: stop online banking time out Pin
metweek5-Mar-09 18:28
Membermetweek5-Mar-09 18:28 
GeneralASP.NET 2.0 Update w/AJAX Support Pin
Tim McCurdy28-Dec-08 15:26
MemberTim McCurdy28-Dec-08 15:26 
GeneralRe: ASP.NET 2.0 Update w/AJAX Support Pin
John Gleeson1-May-09 6:28
MemberJohn Gleeson1-May-09 6:28 
GeneralRe: ASP.NET 2.0 Update w/AJAX Support Pin
Tim McCurdy1-May-09 11:57
MemberTim McCurdy1-May-09 11:57 
GeneralRe: ASP.NET 2.0 Update w/AJAX Support Pin
John Gleeson5-May-09 0:59
MemberJohn Gleeson5-May-09 0:59 
QuestionRe: ASP.NET 2.0 Update w/AJAX Support Pin
thomasabcd2-Jun-09 1:12
Memberthomasabcd2-Jun-09 1:12 
AnswerRe: ASP.NET 2.0 Update w/AJAX Support Pin
Tim McCurdy2-Jun-09 3:00
MemberTim McCurdy2-Jun-09 3:00 
GeneralRe: ASP.NET 2.0 Update w/AJAX Support Pin
thomasabcd2-Jun-09 5:13
Memberthomasabcd2-Jun-09 5:13 
GeneralRe: ASP.NET 2.0 Update w/AJAX Support Pin
koger13-Sep-10 23:58
Memberkoger13-Sep-10 23:58 
GeneralRe: ASP.NET 2.0 Update w/AJAX Support Pin
Tim McCurdy14-Sep-10 3:57
MemberTim McCurdy14-Sep-10 3:57 
GeneralDisable caching Pin
rtitulaer2-Oct-08 6:33
Memberrtitulaer2-Oct-08 6:33 
QuestionWhat about using an ajax call? Pin
Stuart Campbell13-Apr-08 19:54
MemberStuart Campbell13-Apr-08 19:54 
AnswerRe: What about using an ajax call? Pin
Jitu9914-Apr-08 7:41
MemberJitu9914-Apr-08 7:41 
GeneralRe: What about using an ajax call? Pin
Jorge Bay Gondra19-Sep-08 1:47
MemberJorge Bay Gondra19-Sep-08 1:47 
GeneralRe: What about using an ajax call? Pin
Member 307583727-Mar-09 15:02
MemberMember 307583727-Mar-09 15:02 
AnswerRe: What about using an ajax call? Pin
johnlysam16-Jul-09 10:52
Memberjohnlysam16-Jul-09 10:52 
GeneralPlease Help Pin
shetty7714-Jan-08 5:06
Membershetty7714-Jan-08 5:06 

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

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