We help IT Professionals succeed at work.

How to handle web timeout

wsturdev asked
Last Modified: 2010-04-23
In my webconfig I have this:
            <sessionState mode="InProc" stateConnectionString="tcpip=" sqlConnectionString="data source=;Trusted_Connection=no" cookieless="false" timeout="20"/>

My session timeout is 20 minutes.

How do I redirect to a different page?
Can I do some background processing, such as save all entered data, first?
Watch Question

Yes and no. You can execute code by creating a "SessionEnd" event handler in your app's Global class. However, that code will not be able to communicate with the browser/client in any way. By definition, a Session TimeOut means that the client hasn't contacted the server for X amount of time. If you want to avoid losing session-based data, you will need to prevent the users' sessions from timing out. You could do that by increasing the TimeOut value, or by using client-side techniques (e.g. scripting or meta tags) to force the browser to re-connect with the server at intervals (e.g. using a PostBack or an AJAX method). Or you could warn the user (e.g. using a popup) that they need to save soon using client-side script.

However, it is often better to store data that is "being worked on" in ViewState instead of the Session, because ViewState never times out and doesn't consume server resources (except bandwidth). However, there is nothing you can do if the user closes the browser window.

P.S. If you want to use ASP.NET AJAX, you do have some extra options. See this article for details:


Okay, I have some code in Session_End.

When the session times out, nothing happens.  How do I trigger it?

The Session_End event handler is called automatically, you don't trigger it manually. How do you know that the code isn't executing?


I have this instruction in the Session_End handler:


and it does not execute.  At least the user does not suddenly find himself looking at the TimeoutForm.
This one is on us!
(Get your first solution completely free - no credit card required)


Thanks.  I understand now.

I have set a session variable at login and when I migrate to a new page and in the Page Load, if that variable is empty, I know I have timed out and I redirect to a Timeout Message page.  

But you brought up the concept of a client side script that would apparently use a timer and let the user know it is time to save or risk timing out and losing everything.  can you point me to such scripts?


Also, would such a script have to be in every page the user might migrate to?

The way you are doing it now (server side) is a much more reliable way to do it, and is always how I do it. I don't have any examples of a client-side implementation available.

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.