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
- Add(name,value) – Add items to the session state collection
- Clear – Removes All items from a session collection
- Remove(name) – Removes the specified item from the session collection
- Remove All -Removes keys and values from session state
- 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.