Saturday, 31 August 2013

View State in


        provides almost all statemanagement options in a simplified method. Starting from Control level till , Application level it provides all state support. As a web developer it is very important to understand the state requirement properly and apply the available options. To start with statemanagement first we have to manage controls and objects ( variables ) with in the date to retain their values. provides a logic or concept called " VIEWSTATE" for providing controls and variables state.

Controls level:

For controls by default provides state using EnableViewState & ViewStateMode properties we can control the statemanagement options.

Variables level:

For variables we have to explicitly maintain State using ViewStateObject provided by

Q) How maintains state for it's Controls..?  (or) How ViewState for controls work…?

  •   client makes a request for page
  •   Server process the page and concatenates all the result into a string.
  •   This string is converted into base64 encoding format. Which is not encrypted and secured format.
  •   This data is now stored In HiddenField. Which is created by implicitly.
  •   Now, the page is returned to client along with user created control and also HiddenFields.
  •   Now client modify & re-submit the page to server.
  •   Server first off all now reads the hidden fields data. So, that it retains its previously given result.Then start processing the page where old result and new values.
  •   It repeats this cycle for every client as long as client is working with current page.
  •   This result in statefull behavior of page related to controls.

1) In a new form drag & drop a text-box , HTML Text-box, label , Button controls.
2) And in code behind write the below code.
3) Declare a global varriable 
Ex: int a;
4) Write a below code on button click event.

int a;
Protected void btn_Click(object sender,EventArgs e)

5) Then see the output.


1) To maintain state for our variables & objects defined in page code window we have a viewstate property of a page. This property is a reference to a class called "StateBag".

2) This class is responsible for maintaining viewstate concept. So, when we use viewstate object then our data will also be stored as part of " StateBag".

3) Viewstate object can store any type of data, because StateBag class is of Type Object.

4) The only condition for viewstate Data is data should be in Serializable format. Because server has to Serialize& DeSerialize data between request & response.

5) By default viewstate data is not secure, but to make it secure we have a property called EnableViewStateMac is to be true.

Attribute as a part of <%page directive%>.


6) When secure more ustomize in encryption can be perform using MachineKey in web.config.
We can also perform these settings from IIS.

7) With all about consideration viewstate can be use effectively for statemanagement as well as performance.


1) Declare a statebag class, under satebag class write below lines of code.


2) Create a new page and declare label and button control.
3) Write a below lines of code in code behind.

Page Load event:


Button Click Event:

ViewState["a"]= Convert.ToInt32(ViewState["a"])+1;

Q) What type of Data store in ViewState…?

  •  Primitive ---- int,bool……
  •  Complex ---- DataSet,List<> …

ViewState["a"]=ds; ViewState["a"]=List<>;

ViewState  -->  StateBag --> Object  --> any value.

Store any type of data but serialization.


Q) What is Serialization…?

A) The process of converting in- memory form of data into byte format is called 


Q) Why Serialization..?

  • To transfer Data over network.
  • To make object persist.
    Note: By default DataSet is serializable object.


Normal class:

Class Employee
Serializable class:
Class Employee

Note: more complex type in ViewState will reduce the performance of Application. 
Because server has to perform more Serialization & DeSerialization process. 

No comments:

Post a Comment