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
This technique uses Session, Application object.
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
What is VIEW STATE
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.