ViewState in ASP.net.?
In this artical i'm trying to explain about Viewstate in ASP.net.?
Here, i'm trying to
1) explain what is viewstate..?
2) what type of data to be stored in ViewStates..?
3) How viewstate works in Statemanagement concepts..?
4) Serialization and de-serialization..?
VIEWSTATE:
ASP.net statemanagement options:
ASP.net provides almost all statemanagement options in asimplified method. Starting from Control level till ,B>Application level it procides 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 ( varriables ) with in the date to retain their values. ASP.net provides a logic or concept called " VIEWSTATE" for providing controls and varriables state.
Controls level:
For controls by default ASP.net provides state using EnableViewState & ViewStateMode properties we can control the statemanagement options.
Varriables level:
For varriables we have to explicitly maintain State using ViewStateObject provided by ASP.net.
1) 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 behaviour of page related to controls.
Ex:
1) In a new form drag & drop a textBox , HTML TextBox, 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:
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 varriables & objects defind 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();
1) What type of Data store in ViewState…?
A)
1) Primitive ---- int,bool……
2) Complex ---- DataSet,List<> …
ViewState["a"]=ds;
ViewState["a"]=List<>;
ViewState --> StateBag --> Object --> any value.
Store any type of data but serialization.
2) What is Serialization…?
A) The process of converting in- memory form of data into byte format is called Serialization.
3) Why Serialization..?
A) 1) To transfer Data over network.
2) 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.
ViewState
=You can set View State on/off for each control using
EnableViewState property. By default EnableViewState
property will be set to true
=To disable viewstate of a control ,set ViewStateMode property of the control to Disabled.
= we can also disable ViewState of the entire page by adding EnableViewState=false to @page directive
= View state information of all the controls on the page will be submitted to server On each post back.
= When the page is posted to the server, the contents of view state are sent as part of the page post back information. If view state contains a large amount of information, it can affect performance of the page
Request
Load
Postback(viewstate values of controls sended to server)
Render(Encrypted viewstate values comes back & store in hiddenfield)
Unload
Rendering is a task of the ASP.NET server sends HTML (or text in another markup language such as XML or WML) in response to a Web request.
Rendering refers to the process of creating a visual representation on a display surface