ASP.NET

ASP.NET - Managing State

ASP.NET - Managing State

HTTP is a stateless protocol. When the client disconnects from the ASP.NET server ASP.NET discards the page object. 

There has to be a technique to collect information between the request and retrieve it when required.

ASP.NET has four types of state:

  • View state

The view state is the state of the page. When a page is sent back to the user, all changes in properties are reviewed and stored in the _VIEWSTATE.

Viewstate can be enabled or disabled:

  • The entire application enables view state property on the page
  • A page set enable view state attributes.
  • A control by setting up controls.
  • Control state 
    Control state cannot be modified, accessed o could be disabled.
  • Session state
    When users connect to ASP.NET websites a new session object is created every time. When the session is turned on a session state object is created for a new request. Session state is commonly used for storing application data, inventory, supplier list, custom records, etc.

HTTP Session has the following methods

  1. Add(name,value) – Add items to the session state collection
  2. Clear – Removes All items from a session collection
  3. Remove(name) – Removes the specified item from the session collection
  4. Remove All  -Removes keys and values from session state
  5. RemoveAt- Deletes items at a specified index from session state.
  • Application state

This is a collection of all web pages, code, and other files. When information is stored it is available to all the users. ASP.NET creates a state object for each application from HTTPApplicationState.The object is declared in global.asax page.