View state (client side state management in ASP.NET)
- View state is a mechanism by which ASP.Net stores user-specific request and response data between requests. View state is not stored on the server, but is stored in the page itself.
- When the page is submitted to the server the view state is also submitted to the server. The web server pulls the view state from the page to reset the property values and controls when the page is being processed.
- This allows ASP.Net to have all the object data available to the web server without having to store it on the server. This results in a scalable web server which can handle more user requests.
- Imagine a scenario where a user can edit some profile information. When processing the request the page might have to get information from the database.
- In this case the data in various controls and the property values are stored in the view state. Suppose there is a problem with the submitted data and the page is returned back to the browser.
- You can see that the controls are populated with the data you entered and the property values are preserved. This is because the data is wrapped up into view state with your request and ASP.Net client-side state management features takes care of all this work for you.
- Unless disabled, view state is a part of every ASP.Net page. The view state values are hashed, compressed and encoded for Unicode implementations which provide better optimization and security than simple hidden fields.
1: Example of view state
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwUKMTY1NDU2MTA1MmRkqIBFV24wUH0Ny5RdjT5FltHvW5EIx6oxq6sSsk4aVOk=" />
View state is available as a hidden field as shown above. You can view source of any .Net page and you will come across a hidden field as above. Writing data to view statethis.ViewState.Add(“customdata”,”I am storing some data in view state”);Reading view state datastring customdata=(string)ViewState[“customdata”];
- Data is stored as objects in View state, so you should cast it to the correct data type before using it. You can store a wide variety of data in view state.
- Any object that is marked serializable can be added to view state.
- Adding data to view state is great when you want to preserve information with page postback. However it does not transfer information from one page to another.
ADVANTAGE:



No comments:
Post a Comment