Updated April 10, 2023
Introduction of ASP.NET State Management
Hyper Text Transfer Protocol is a stateless protocol. When client disconnect from the server, asp.net engine removes the pages objects. This way each web application can balance up to serve several requests concurrently without running out of server memory. But, there need to be some technique to store the information between request and to retrieve it when required. This information is the current value of all the controls andvariables for the current user in the current session is called the state. asp.net includes number of features for managing these state at server side and client side. View state, cookies, Hidden fields and Query string (URL) are used at client side. Session state, Caching and application state are performed at server side.
State Management in ASP.NET
To manage state, asp.net provides number of features. Let’s discuss some of these features.
- State Management Using View State: View state is all about the state of the page and its controls. It is automatically maintained across the posts by asp.net framework. When page is sent back to the client for output, the changes in the properties of the page and its control are determined and stored in the value of hidden input field named _VIEWSTATE. When page is again post back to the _VIEWSTATE field is sent to the server with HTTP request. View state is implemented using view state object defined by the StateBag class which defines collection of view state items. The StateBag class is data structure containing attribute or value pairs, stored as string associated with objects. User can set the view state as enabled or disabled for entire application or page. For entire application user set the EnableViewState property in the <pages> section of web.config file. For page user set the EnableViewState attribute of the page directive as <%@Page Language = “C#” EnableViewState = “false”%>.
- State Management Using Session State: When user connect to an aso.net website, new session object is created. When session state is turned on, a new session state object is created for each new request. This session state object becomes part of the context and it is available through the web page. Session state is generally used for storing application data for user like inventory or supplier list, or customer record or shopping cart. It can also keep information the web site user and his preference and keep track of pending operation. Session are identified and tracked by SessionID which is passed from client to server and back as cookie or. modified URL. SessionID is unique and randomly created. HttpSessionState class is used to create the object of session state which defines collection of session state items. SessionID, Item, count and TimeOut are the properties of HttpSessionState. Add, Clear, Remove, RemoveAll, RemoveAt are methods of HttpSessionState.
- State Management Using Application State: Asp.net application is the collection of all web pages, code files and other files within single virtual directory on web server. When information is stored in application state, it is Available to all the users of that application. asp.net creates the application state object for each application from the HttpApplicationState class and stores this object in the server memory. This object is represented by class file called as global.asax. Application state is mostly used to store the web site hit counters and other statistical data, global application data like tax rate, discount rate and to keep track of users visiting the site. HttpApplicationState class gas two properties such as Item and count. Add, Clear, Lock, Unlock, Remove, RemoveAt, RemoveAll are the properties of the HttpApplicationState..
- State Management Using Cookies: Cookies is a small piece of information stored on the client computer. It is used to store the preference information kike User name, Password, City, Phone number, Emil Id, etc on client computer. To use the cookie, user needs to import the namespace called as System.Web.HttpCookie. There are two types of cookies Persist cookie, and Non persist cookie. Persist cookie is a cookie which does not have expired time. Non Persist cookie has expired time.
- State Management Using Query String: In any web application, transmission of data from one page to another is an essential part. Query string is used to transfer data from one page to another page. Querystrings are simply the data that is appended to the end of web page URL. User can view the values which query string holds without using special operations kike ViewSource. If the number of parameters increases, thenit becomes bulky and often difficult to maintain code using QueryString. It is visible in your address part of your browser so you should not use it with sensitive information. It cannot be used to send & and apace characters.
This is a guide to ASP.NET State Management. Here we also discuss the introduction and state management in ASP.NET along with features. You may also have a look at the following articles to learn more –