State Management in ASP.NET

In this article, we will understand state management in ASP.NET in detail.

What do you mean by Sate Management?

State Management is a process by which state and page information is maintained over multiple requests for same or different pages. As HTTP is a stateless protocol, server does not store any information once the

response is sent back to client based on his request. When user submits request again, the server treats it as a new user. This is called stateless model. This model was workable when static web sites were developed and hosted in the past. Now, with interactive web sites or dynamic web site, there is a need to preserve some information to identify user, interact with user again and again within same session and same application. This concept is known as Stateful protocol. The information can be related to user, data objects, web pages or server objects.

To support this kind of model, ASP.NET provides two types of State Management techniques, server side and client side as shown in Figure.

State Management in ASP.NET

Server Side State Management Options

ASP.NET provides facility to save information on server side as well as in client side. The following options are available in Server Side State Management.

  • Application State: Application state allows saving of data at application level which is accessible throughout the life of an application. The life of application starts when IIS is started and ends when IIS is stopped.
  • Session State: The session state persists till the user is active or session time expires. When a user submits request, a session is started. The session gets over either when time expires or user explicitly abandons the sessions. The required information can be saved in session for later user by same user within the same application.
  • Profile Properties: This option also allows saving of user specific data. It is similar to Session state except the data stored in Profile state never expires on use this property, the SQLProfileProvider class needs to be configured. It allows storing of data in SQL database. Since data is stored in database and not in application memory, therefore there is no risk of losing data even if IIS is restarted again and again.
  • Cache: Caching is a technique by which frequently used data and web pages are stored in cache so that repeated cost of retrieval can be avoided. Storing frequently used data in cache ensures high availability, improved performance and scalability. Cache is object of System.Web.Caching Cache class. The main disadvantage of using Cache is that it is unreliable. The previously stored data stored in cache is deleted automatically to meet memory requirement of current process.

Client Side State Management Options

The options available in client side state management help in storing information either in the page or at the client computer. No information is stored at server side. The followings options are used for client side state management.

  • View State: View state provides facility to preserve page and values of controls at the client side. The information is stored after post back. Post back is a request from user for the page which is not for the first time. If value of IsPostBack property is true, it means page is not requested for the first time. The view state can be at page level, application level, machine level and control level. In page level state management, as long as the user is on current page, the information is retained. Whenever user submits form, the current state of page and controls are hashed into a string and saved in hidden field of the page. More than one hidden field can be used if data exceed limit set by MaxPageStateFieldLength property. When page is sent to server, the page parses the view state string and restores the information. This is default mechanism. The view state can be disabled at any stage. The property EnableViewState="false" is used in code when it is required to disable view state

The demonstrate the concept of view state option, consider an ASP.NET project haring one ben Each time a button is clicked, it displays the number of times the button is clicked.

  • Control State: This is another client side state management option. This is used when there is a need to store control data related to Custom control. View state can be disabled but control state cannot be disabled.
  • Hidden Field State: ASP.NET allows storing information in a hidden field which is a server control and can be used to store information at page level. The value of the hidden field is sent to HTTP form collection along with value of other controls. The hidden file can be created in source file as given below.<input type="hidden" id="username" name="username" value=""

This hidden field can be accessed in code behind file as given below. Dim st as String = Request QueryString("username")

  • Cookies: Cookie is a small amount of information that is stored in client machine.
  • QueryString: A QueryString contains the information that is sent to server with URL.

Next Topic#




Latest Courses