17 May 2007

View State

Introduction
-------------

The web is a state less medium - state is not maintained between client requests by default. ASP.NET is providing a facility to maintain this.
Page level state is information maintaned when an element on the web form page causes a subsequent request to the server for the same page - referred to as "postback".
The control.viewstate property is associated with each server control in your web form and provides a dictionary object for retaining values between such multiple requests for same page. This is the method that the page uses to preserve page and control property information in the page.
When a page is re-loaded two methods pertaining to viewstate are called: LoadViewState and SaveViewState.

Post back controls like text box and non post back controls like label controls.

ViewState is not limited to the storage of simple values, you can use viewstate to store any object as long as it is serializable.

Session State or ViewState?

There are certain cases where holding a state value in ViewState is not the best option. The most commonly used alternative is Session state, which is generally better suited for:

Large amounts of data.As ViewState increases the size of both the HTML page sent to the browser and the size of form posted back, it's a poor choice for storing large amounts of data.

Sensitive data that is not already displayed in the UI.While the ViewState data is encoded and may optionally be encrypted, your data is most secure if it is never sent to the client. So, Session state is a more secure option.

Objects not readily serialized into ViewState, for example, DataSet.As already discussed the ViewState serializer is optimized for a small set of common object types. Other types that are serializable may be persisted in ViewState, but are slower and can generate a very large ViewState.

No comments: