Friday, February 19, 2010

.NET: View State & Control State Overview

View State:

The ViewState property provides a dictionary object for retaining values between multiple requests for the same page postbacks.
ASP.NET page framework uses by default to preserve page and control property values between round trips.
When the HTML for the page is rendered, the current state of the page and values that need to be retained during postbacks are serialized into XML and then encoded using base64 encoding strings and output in the view state hidden field or fields.
EnableViewState is always set to "TRUE" in ASP.NET.

<input type="hidden" name="__VIEWSTATE" value="dDwtMTI4MDAxOTkwMjs7bDxJbWFnZUJ1dHRvbjE7Pj69/Bmk435QA9XEXIwU10D6d8QwoQ==" />

Amount of data in a __VIEWSTATE hidden field is significantly becomes large depends on your page controls objects, some proxies and firewalls will prevent access to the page that contains them. The best practice to set EnableViewState = false at Controls level or Page level depending on the requirement which could potentially increase your page access time.

For instance if you have place a DataGrid control on your page that's bind to the result of a simple query , check the view source of running instance of page and you will probably be shocked to see the amount of __VIEWSTATE hidden field data. So deciding when to disable view state in your controls is obviously important factor.

MaxPageStateFieldLength : (This property is new in the .NET Framework version 2.0.)
When the MaxPageStateFieldLength property is set to a positive number, the view state sent to the client browser is broken into multiple hidden fields, and each field's value is less than the size specified in the MaxPageStateFieldLength property.

You could always set MaxPageStateFieldLength property in the pages element of the Web.config file.
ASP.NET 2.0 enables to split the ViewState's single hidden field into several using the MaxPageStateFieldLength property in the web.config <pages> section. This indicates the maximum bytes size allowed for one viewstate hidden field. If the real size exceeds the value then viewstate is splitted in multiple fields. By default, the attribute is set to -1 which means that no maximum size is defined.

Sample ViewState before:

Then set in web.config:
Sample ViewState After :
<input type="hidden" name="__VIEWSTATE1" id="__VIEWSTATE1" value="KwANAGQYAQUJR3JpZFZpZXcxD2dk4sjERFfnDXV/" />
<input type="hidden" name="__VIEWSTATE2" id="__VIEWSTATE2" value="hMFGAL10HQUnZbk="
Data Types You Can Store in View State:
You can store objects of the following types in view state:
  • Strings
  • Integers
  • Boolean values
  • Array objects
  • ArrayList objects
  • Hash tables

Control State:

View state is a property inherited from the control class and is inherited by all controls. It is independent of the Control's state.
Control state is the control's private view state. Control state is designed for storing a control's essential data (such as the page number of a pager control) that must be available on postback when view state is disabled.

View state can be enabled or disabled for an entire page or for a specific control but Control state cannot be disabled for a control.
The control state is stored in the same hidden variable as the view state. Even when view state is disabled control state travels to the client and back to the server in the page. On postback the hidden variable is deserialized and the control state is loaded into each control that is registered for the control state mechanism.

 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 is selected between round trips. The ViewState property can be used for this purpose, but view state can be turned 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.

No comments:

Post a Comment