Browse DevX
Sign up for e-mail newsletters from DevX


Client- and Server-side Solutions to Managing State in ASP.NET : Page 6

Whether you are building a traditional Windows application or a Web-based application, state is what an application knows about the user, their current interaction with the application, and other pieces of global information. Get a full assessment of the best techniques for managing state.




Building the Right Environment to Support AI, Machine Learning and Deep Learning

Server-Based Technique #2: Session State
The second server-based technique involves using session state variables. Since each user is assigned their own unique session when they initially visit a site, think of session state variables as memory variables stored by the Web server local to the current user. They can store information about the user, what tasks they have completed, information in a shopping cart, etc. You can add objects of any type to session state.

You can add values to the session state with code like this.

Session("UserName") = "Jim Duffy" Session("Company") = "TakeNote Technologies"

And you can retrieve session state variables like this.

Dim Company As String = Session("Company")

And you probably guessed that you can remove session state variables with code like this.


You can remove the variables from session state by using the RemoveAll method like this


A session starts when a user accesses a page on a Web site for the first time, at which time they are assigned a unique session ID. The server stores the user's session ID in the Session.SessionID property. The session ID will be unique for the current session but it is not guaranteed to be unique over time.

A session ends after a pre-defined period of inactivity elapses. Once the server decides that the user has left it removes all session state information for that user. The default session timeout period is 20 minutes. If the user requests another page after 20 minutes has elapsed the server will treat them as a new user. You can change the session timeout value in more than one way. For instance, you can adjust the Session.Timeout property like this.

Session.Timeout = 10

You can also adjust the sessionState timeout setting in the Web.config file.

<configuration> <system.Web> <sessionState timeout="10" /> </system.Web> </configuration>

You can call the Session.Abandon method to end a user session. If a user then tries to access a page the server will assign them a new session ID and it will clear all the previous session variables. You'll typically use Session.Abandon on sign-out pages.

There are a number of events related to the Session object contained in the global.asax file. Discussing these events is beyond the scope of this article but I suggest you take a look at them and understand what they do and when they are raised.

Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date