Saturday 31 August 2013

View State in ASP.net..

VIEWSTATE:


 
                 ASP.net 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. ASP.net provides a logic or concept called " VIEWSTATE" for providing controls and variables state.

Controls level:

For controls by default ASP.net 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 ASP.net.

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


A)
  •   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 ASP.net 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.

Ex:
 
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.
EX:

int a;
Protected void btn_Click(object sender,EventArgs e)
{
         a++;
         lbl.Text=a.ToString();
}

5) Then see the output.

VIEWSTATE Object:


 
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 ASP.net " 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%>.


Ex:
EnableViewStateMac="true/false"

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.


Ex:

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

Page.ViewState["a"]=10;

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

Page Load event:

If(!IsPostBack)
{
  ViewState["a"]=0;
}

Button Click Event:




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



Q) What type of Data store in ViewState…?

 
A)         
  •  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 
Serialization.

 

Q) Why Serialization..?


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

Ex:

 
Normal class:


Class Employee
{
}
 
 
Serializable class:
 
[Serilizable]
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