Sunday, December 16, 2007

ViewState

Viewstate persists the state of object across postbacks.
When the page is processed, the current state of the page and controls is hashed into a string and saved in the page as a hidden field. When the page is posted back to the server, the page parses the view state at page initialization and restores property information in the page.

By default viewState is enabled . If you view a web form page in your browser you will see a line in your rendered HTML:

value="abJHnm76hdjsdGFYDFDHDHFbcdy6767ffvfHxdHbf6879gHJge2" />

Page level state is maintained automatically by ASP.NET but you can disable it by setting the EnableViewState property to false for either the controls and page as a whole.
Where to use ViewState and where not..
  1. Amount of data: ViewState increases the size of both the HTML page sent to the browser and the size of form posted back. Use Viewstate where you want to store small amount of data.
  2. Security: ViewState data is encoded and encrypted but getting displayed it to the client is not at all safe in case of sensitive data .

No comments: