Before writing this article what I know about view state is
It is one of the statemanagement techniques and its its scope is with in the page
Then I spent some time on viewstate and realized its importance.
I have a aspx page with the following code
Code Section-1
<asp:TextBox ID="TextBox1" runat="server">asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
Now I type some text in textbox(Hello india!) and and click on button.now page refreshes i.e it posts the request to the server.
Once that is done you can still see the text in textbox(Hello India!).How is this possible?
We all know the http protocol suffers from memory loss and don’t remember the prevoius requests,still we are able to retain the value which is posted to the server.
This is possible by our hero “ViewState”.
So I define view state in the following way
Viewstate is a technique/process by which we can retain the values of the page between the subsequent postback.
Ok now let us go still futher and see
What happens in the page lifecycle of Asp.net page.
This entire concept of the view state lies in this image which is taken from MSDN.
Now consider that we requested a page which is mentioned above in codesection1 from the browser.
The server presents the page requested on to your browser.Now you type some text in textbox(Hello india!) and click on the button.
Now look at the image,
Intialization:
This is the first step ,here heirarchy of the controls are set,for example which control should come first and which is the next,so moreover the order of the controls.
Consider a girdview ,a gridview is not a single control,it inturn will contain child controls in it,so this heirarchy of child,parent controls is set in this part of intialization.
Now we are done with intialization.
Load View State:
The data stored in viewstate can be seen by the end user.Now right click on your page and click on viewsource,in the source you can see some thing like the below
This is how the viewstate is stored for your page.This is in Unicode format remember it is not encrypted it is only in Unicode format.
A page will have one hidden control with id & name _viewstate which will contain all the information of the controls in that page as value.
So this value increases as the number of controls increases.So even this will be performance issue when this content increases.Because the time taken to load the page increases as the view state infomartion increases.
Infact there are ways where we can restrict this content,we will discuss this in next article.
Now lets come back now what this load viewstate does?This will load all the content in to the hidden field.This will happen only on postbacks.
Load Postback Data:
Now we have viewstate information with us in this stage so called LoadPostback Data,all the controls are loaded with viewstate information from the hidden field.
Load Event:
Now all the controls are loaded at this stage.
RaisePostback Event:
In asp.net there are 2 types of sever events
1)Change events (ex:selection changed events,textchanged events)
2)Raised Events(Button clicks)
So postback is raised
SaveViewstate:
In this stage all the controls information is saved to view state as the name itself suggests Saveviewstate.
Render:
The last stage is Render which will present you the output on the screen.
So at glance
Viewstate information is stored in the hidden fields , as httpprotocol has memeoryloss ,in the subsequent postbacks the values are retained from the view state.
Now as I said we have some performance issues with viewstate,so consider a gridview which is readonly and we are showing all the employees data.
Let us consider we have some 1000 employees so if all these information is stored in viewstate then time to load the page and memory increases and piles up.
So The better approach in this case to disable the viewState.
Now question is how to disable viewstate?
Every asp.net server control has a property called “EnableViewState=True/False”
Even the viewstate can be disabled at page level,it can be done at the page directive at the top of the aspx page
<%@ Page Language="C#" AutoEventWireup="true" EnableViewState="false" CodeFile="Default.aspx.cs" Inherits="_Default" %>
Now Next Question is When I disable viewstate for a textbox ,still if I click the button the value is retained back in the textbox,how is this possible.
These controls do not use ViewState to preserve their state. These controls post their values with the form (form post collection) and therefore they can keep their state without ViewState (they implement IPostBackDataHandler interface).They do have use for ViewState, but to detect if value was changed between postbacks. Disabling ViewState works 100% for controls who are completely dependant on ViewState to keep their state over postback.
Plain DataGrid or DataList are examples of such controls.
please free to share our comments.