State Management in ASP.Net



State Management is defined as the management of the state of one or more user interface controls such as textboxes, buttons, etc in a Graphical User Interface. In UI programming technique, the state of UI control depends on the state of other UI controls. For example, a state managed UI control such as a command button will be in its enabled state when input fields or the text fields have valid values and the button will be in the disabled state if the input fields are empty or have invalid values.

In ASP.NET applications, hosted in a web server are accessed over the stateless HTTP protocol. As such, if the application uses state full inapplication statteraction, it has to implement state management on its own. ASP.NET provides various functionality for state management in ASP.NET applications.

Some of them are

1. Application state

A collection of user-defined variables which are shared by an ASP.NET application are termed as Application state. When the Application_OnStart event fires at the time of loading of the first instance of the applications these variables are set and initialized and are available till the last instance of the application exits. To access the Application state variables the Applications collection is used, which provides a wrapper for the application state variables.

2. Session state

A collection of user-defined session variables are termed as Session state, which are persevered during a user session. These variables are accessed using a Session Collection. These variables have a unique instance to different instances of a user session for the application. Session variables can be set to automatically destroyed after a definite period of inactive time, irrespective of the session state whether session ends or not. At the client side, a user session is identified either by a cookie or by encoding the session ID in the URL.

ASP.NET supports three modes of persistence for session variables:

  • In Process Mode

    These session variables are maintained within the process of ASP.NET Applications. This is the fastest way, however, in this mode the variables are destroyed when the process is recycled or shut down. This mode is not recommended for critical applications as the application is recycled from time to time.
  • ASPState Mode

    In this mode, A separate Windows service is run by ASP.NET, Which maintains the state variables. This has a negative impact on performance as the state management happens outside the ASP.NET process,on the other hand it allows multiple ASP.NET instances to share the same server state, thus allowing an ASP.NET application to be load-balanced and scaled out on multiple servers.As state management service runs independent from ASP.NET, variables can persist across ASP.NET process shutdowns.
  • SqlServer Mode

    In this mode, the state variables are stored in a database, which are accessible using SQL or SQL Queries. Variables can be persisted across ASP.NET process shutdowns. The advantage of SqlServer mode is it allows the application to balance load on a server cluster while sharing sessions between servers.
View state

Mechanism to manage the state at the page-level is refereed as View state, that is utilized by the HTML pages by ASP.NET applications to maintain the state of the web form controls. The encoded state of the controls are sent to the server at time of form submission in a hidden field known as __VIEWSTATE. The server returns the value back of the variable so that when the page is re-rendered, the controls render at their last state.

Other

Other means of state management that are supported by ASP.NET are cookies, caching, and using the query string.

Up Next
    Ebook Download
    View all
    Learn
    View all