What is Viewstate?

Showing Answers 1 - 4 of 4 Answers

Jimmy

  • Sep 10th, 2005
 

The web is a stateless medium - state is not maintained between client requests by default. Technologies must be utilized to provide some form of state management if this is what is required of your application, which will be the case for all but the simplest of web applications. ASP.NET provides several mechanisms to manage state in a more powerful and easier to utilize way than classic ASP.

Page level state is information maintained when an element on the web form page causes a subsequent request to the server for the same page - referred to as 'postback'. This is appropriately called ViewState as the data involved is usually, though not necessarily, shown to the user directly within the page output.

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 the same page. This is the method that the page uses to preserve page and control property values between round trips.

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 string at page initialization and restores property information in the page.

ViewState is enabled by default so if you view a web form page in your browser you will see a line similar to the following near the form definition in your rendered HTML:

ViewState offers a substantial improvement over the two competing techniques for state management via the client: standard hidden fields and cookies, in that ViewState is not limited to the storage of simple values. You can use ViewState to store any object as long as it is serializable, and the standard VB.NET types are. Serialization is the process of storing an object's data and other information necessary to reconstruct the object later.

There is a further complication: a type that either is serializable or has a TypeConverter defined for it can be persisted in ViewState. However, types that are only serializable are slower and generate a much larger ViewState than those that have a TypeConverter. The TypeConverter class provides a unified way of converting types of values to other types, as well as for accessing standard values and subproperties.

ViewState is serialized using a limited object serialization format that is optimized for primitive types, and for String, ArrayList, and HashTable types. You may want to consider these issues if page performance concerns are key

Reference : Click here

  Was this answer useful?  Yes

Jimmy

  • Sep 10th, 2005
 

The view state is a key element of an ASP.NET page because it is the primary means to persist the state of the Web server controls. Whenever the page posts back, the state is restored, updated using the current form parameters, then used to run the postback event handler. Normally, the view state is a hashed string encoded as Base64 and stored in a hidden field called __VIEWSTATE. In this way, the view state is not cached on the client, but simply transported back and forth with potential issues both for security and performance. Since it is performance overhead, you need to decide properly when and where you should use viewstate in your webform.

  Was this answer useful?  Yes

Kiran

  • Sep 10th, 2005
 

All controls in ASP.NET inherit the View State property from the Control Class. The state is stored in Name/value pairs and the view state property returns an instance of the StateBag class which is a specialized dictionary object. At a level of abstraction the Control state and the View state are similar but have several differences in handling.

View State

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

Control state cannot be disabled for a control.

View state is sent by the server as a hidden variable in a form, as part of every response to the client, and is returned to the server by the client as part of a postback. However, to reduce bandwidth demand when using mobile controls, ASP.NET does not send a page's view state to the client. Instead, the view state is saved as part of a user's session on the server. Where there is a view state, a hidden field that identifies this page's view state is sent by the server as part of every response to the client, and is returned to the server by the client as part of the next request. However, since the view state for a given page must be kept on the server, it is possible for the current state to be out of synchronization with the current page of the browser, if the user uses the Back feature on the browser to go back in the history.

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.

The custom control?s view state is stored by adding to a dictionary object through the ViewState property which is inherited from the Control Class. The ViewState property returns an object of the type StateBag which is a dictionary like type.

In the control state the objects are not stored in a fixed container. The data can be maintained in plain private or protected members. This makes access to data faster and is not mediated by a dictionary object.

Few server controls make private use of the view state and even in those controls the use is limited to specific features.

All controls can use the control state as it is intrinsic to the control.

If a developer wants a control to persist in its state between posts, the view state has to be used privately to store the content of non public properties. However, this will not work if the view state is disabled or the application or page level settings are changed.

The control state replaces the private use of the view state by storing the state within the control.

It is possible to control the amount of data that is moved back and forth between posts.

It is not possible to control the amount of data that is moved back and forth during posts.

The view state of the control/page is automatically initialized and restored by ASP.NET runtime

The developer has to initialize the private state of a control on loading.

ASP.NET 2.0 Free Tutorials : State Management in ASP.NET 2.0

  Was this answer useful?  Yes

Swapna

  • Sep 12th, 2005
 

ViewState allows the state of objects (serializable) to be stored in a hidden field on the page. ViewState is transported to the client and back to the server, and is not stored on the server or any other external source. ViewState is used the retain the state of server-side objects between postabacks.

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions