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.
For controls by default ASP.net provides state using EnableViewState & ViewStateMode properties we can control the statemanagement options.
For varriables we have to explicitly maintain State using ViewStateObject provided by ASP.net.
1) How ASP.net maintains state for it's Controls..?
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.
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.
Protected void btn_Click(object sender,EventArgs e)
5) Then see the output.
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%>.
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:
1) What type of Data store in ViewState…?
1) Primitive ---- int,bool……
2) Complex ---- DataSet,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.
Note: more complex type in ViewState will reduce the performance of Application. Because server has to perform more Serialization & DeSerialization process.
=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
Postback(viewstate values of controls sended to server)
Render(Encrypted viewstate values comes back & store in hiddenfield)
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
Hai koti Balaji,
You are right.. if we transfer large amount data performance decreases. But tell me one thing fetch data from DataBase is good one or store that object in viewstate and fetch that viewstate data is good one. As of my knowledge fetching data from view state or sessions is good compare to call DataBase. Compare to that Viewstate performance also good.
Hope you understood if you have any doubt please revert back..
You can set Page and Control Level ViewStateMode=”Disabled” by use asp:Label ID="ViewStategridlebel1" ViewStateMode="Disabled" runat="server"
Set disable control level in view state mode by using this ViewStateMode=”Disabled” and Control Level ViewStateMode=”Enabled”
Thank you Phagu Mahato,
For giving your valuable information...