State Management Policies
- Whenever we visit a website, our browser communicates with the respective server using the HTTP or HTTPs protocol.
- As HTTP is a stateless protocol. It just cleans up or we can say removes all the resources/references that were serving a specific request in the past.
- These State Management Policies resources can be:
- Allocated Memory
- Sessions ID’s
- Some URL info
- and so on.
- In traditional Web programming, this would typically mean that all information associated with the page and the controls on the page would be lost with each round trip.
- To overcome this inherent limitation of traditional Web programming, ASP.NET includes several options that help you preserve data on both a per-page basis and an application-wide basis.
- The state of a web application helps you to store the runtime changes that have been made to the web application.
- For example, a user selects and saves some products in the shopping cart of an online shopping website.
- For that you have to store it to state, otherwise, the changes are discarded.
State Management Types
View State (Page Level State)
- The ViewState property provides a dictionary object for retaining values between multiple requests for the same page.
- This is the default method that the page uses to preserve page and control property values between round trips.
- The page framework uses view state to persist control settings between postbacks.
- You can use the view to do the following:
- Keep values between postbacks without storing them in session state or in a user profile.
- Store the values of page or control properties that you define.
- Create a custom view state provider that lets you store view state information in a SQL Server database or in another data store.
- Some of the features of view state are:
- It is page-level State Management
- Used for holding data temporarily
- Can store any type of data
- Property dependent
- Sometimes you need to store control-state data in order for a control to work properly. For example, if you have written a custom control that has different tabs that show different information, in order for that control to work as expected, the control needs to know which tab selected between round trips.
- The ViewState property can used for this purpose, but view state can turn off at a page level by developers, effectively breaking your control.
- To solve this, the ASP.NET page framework exposes a feature in ASP.NET called control state.
- The ControlState property allows you to persist property information that specific to a control and cannot turn off like the ViewState property.
- NET allows you to store information in a HiddenField control, which renders as a standard HTML hidden field.
- A hidden field does not render visibly in the browser, but you can set its properties just as you can with a standard control.
- When a page submitted to the server, the content of a hidden field sent in the HTTP form collection along with the values of other controls.
- A hidden field acts as a repository for any page-specific information that you want to store directly on the page.
- HiddenField control stores a single variable in its Value property and must explicitly added to the page.
- Some features of hidden fields are:
- Contains a small amount of memory
- Direct functionality access