O'Reilly logo

Professional ASP.NET 3.5 AJAX by Matt Gibbs, Dan Wahlin, Bill Evjen, Dave Reed

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

14.2. Understanding Sessions

Sessions within an ASP.NET application enable users to easily maintain application state. Sessions will remain with the user as he works through repeated calls to an application for a defined period.

Sessions are easily created, and it is just as easy to retrieve information from them. Use the following code to create a session for the user or calling application that can be accessed later in the application or to assign a value to an already established session.

Session["EmployeeID"] = Value1;

This will assign what was being held in the variable Value1 to the EmployeeID Session object. To retrieve this information from the session and then use it in your code, do the following:

Value2 = Session["EmployeeID"];

In ASP.NET, session timeout is similar to what it was in classic ASP — a session would timeout on the user after 20 minutes. If the user opened a page within a Web application (thereby creating a session) and then walked away for a cup of coffee, when the user came back to the page 40 minutes later, the session would not be there for them. You could get around this by going into the server and changing the time allotted to the session timeout property, but this was cumbersome and required that you stop the server and then start it again for the changes to take effect. In addition, because sessions are resource intensive, you would not want to store too many sessions for too long.

With ASP.NET it is now possible to change the session timeout property ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required