ViewState and User Defined Data Type

Introduction

In this article, I would like to share some of the concepts about ViewState.

State Maintenance

The web is stateless because the HTTP protocol is a stateless protocol. By default, state is not maintained between different client requests to the server by the application. But some technologies are provided to maintain the state. We have three different types of state:

Page level

User level

Application level

ViewState is used to maintain page level state. Rest of the two are out of this article.

In ASP.NET, Control.ViewState property has been given to each control for retaining its values between round trip server requests.

How the ViewState is working

If ViewState property of a web form is enabled then we can see a segment of HTML code in browser window by right clicking the mouse and selecting View Source. It would be like:

A hidden control is added to each page and all controls' data are stored and send to client’s browser. When page is reloaded, two methods of ViewState are called: LoadViewState() and SaveViewState(), and data will be loaded into corresponding controls.

What we can store in ViewState?

We can use the ViewState to store any type of object, if that object is serializable or is having TypeConverter. ‘Serialization is the process of converting object’s data into a stream based output, and using that output later we can construct the same object’. Here, we must be aware of where this serialization plays its role in ViewState. As early, we seen that controls' data are stored into a hidden field, before that the value or data is being serialized.

User Defined DataType

Users can define their own data types. Classes and structure definitions fall under user defined data types. If we want to play with user defined datatypes and ViewState, then really we need to concentrate on defining them.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.