A Beginner's chapter to learn View State

We know HTTP is stateless protocol hence it cannot persist values across page postback. To overcome this problem statemanagment technique is in action. Viewstate is one of the client side state management technique used to persist web form values during postback. This article will give you idea about Viewstate, its usage and syntax.

Every Web based project need to Persist values while page postback. We use State Management for that task.
Here we have a short trip of State Management
There are two types of State Management

1. Serverside State Management
1. Clientside State Management

Serverside statemanagement
This technique uses Session, Application object.

Clientside statemanagement
This technique uses Cookies, QueryString, Viewstate, Hidden Fields that are used to persist data while page postback.
I have already explain different types of state management and it usage, you can check them on below links

This article or we can say its a small technical tip will give you a idea about a VIEWSTATE and its usage. so let ready for it. This hour for VIEWSTATE

VIEW STATE is good client based state management Technique to persist date while Page is postback. It is the process of Sending and Receiving information from Server
Every ASP.NET page provides built-in property as a ViewState for automatically storing values between multiple requests for the same page. ViewState is the mechanism by which we can maintain Web page object and child control object data between page postback. All controls are stored in Viewstate before they are posted back to server.
All the controls that are stored in viewstate are then stored to hidden field with some standard encryption technique, if you see your page view source then
you will see a hidden field with the name "__ViewState" and a encrypted data in it's 'value' field. Data of each controls is hashed into a string and saved in the page as a hiddenfield, if data is more in size then hashed values are divided in multiple hidden fields.

look at the below snap, which will tell you how it will store Viewstate as hidden field in encrypted format.

Storing values in View State
we can also store the values in view state and main them between postbacks. here is the way to store values in view state.
View state has same code like Session / Application veriable


Here string "TEST" is stored in "Vari1" variable

following line shows how to collect values from View state

string val1 = ViewState["Vari1"]

view state store a object type and can store any type of data so we can store Dataset, DataReader, Connection or any type of object in it.
see following code snippet to check how to store Dataset in View state, it may be useful while implementing gridview paging functionality

Dataset objDS = new Dataset();
//fill dataset here from DB
//now store it in view state
viewSate["Dataset"] = objDS;

//now after page postback retrieve it back and store it in Dataset object
objDS = (Dataset)viewSate["Dataset"];

Limitation of View state
- Adding lots of data to ViewState can cause performance problems because the data is sent to the browser when a page is requested to postback.
- To minimize the ViewState data, we can set the server controls EnableViewState property to false but we after that we can't able to render the control we have to manually add it to control set, that is we need to write code to repopulate the control yourself between page calls.
- View state cannot persist data between multiple postback, so clearly we cannot send data from one webform to another webform using ViewState

This is all about the viewstate.

Suggestion and feedback most welcome.

Thanking You


No responses found. Be the first to comment...

  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name: