C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Articles » .NET Framework »

ViewState - behind the scenes


Posted Date: 30 Apr 2006    Resource Type: Articles    Category: .NET Framework
Author: Tony JohnMember Level: Diamond    
Rating: 1 out of 5Points: 10



What is ViewState ?



ViewState is used to store the 'state' of the object in a hidden field on the page. ViewState is sent to the client browser and back to the server, and is not stored on the server. ViewState is used the retain the state of server-side objects between postabacks.

How to use ViewState?



To use the ViewState, you do not need to do anything specifically. Unless you disable it, the ASP.NET framework will add a hidden filed called __VIEWSTATE to your web page. When displayed on a browser, you can right click on the page and view the source. The source will show an entry similar to shown below:

<input type="hidden" name="__VIEWSTATE" value="zSkwjhClKsuaHJSDTmshtSHmomSDbm237NSSJd8sdbj" />

The value part looks like some junk characters. This is encrypted value. The state of all web controls are encrypted and stored here. When you submit the page back to the server, the ASP.NET Framework will retrieve the values of each web controls and assign to them so that you do not need to re populate it.

For example, if you have a DropdownList box in the web page and you populate some values in that from database. When you submit the page to the server by pressing some submit button in the page, the framework will retrieve the values of the Dropdownlist from the ViewState and re populate the drop down automatically. You do not need to re populate it from the database. This works for all web controls.

In old ASP days, you had to re populate all the controls every time you submit to the server.

Can I disable View State?



Yes, you can disable the ViewState of any control by setting the property EnableViewState = False for the control.

You can disable the ViewState for all the controls in a page by setting the property EnableViewState = False for the Form.

Why should I disable ViewState? Is it reccommended to disable ViewState?



It depends on your requirement. ViewState comes with a penalty. It adds the state of all the webcontrols to a hidden field in the page, increasing the size of the page. This may have an impact on slow connections.

You should disable ViewState for the Form for all the pages which do not have any user input controls (All read only pages).

For all the pages which have user input controls (like Textbox, DropdownList etc), you should take an appropriate decision. If you need to retain the values in the controls during postback, you must enable ViewState for such controls. Disable ViewState selectively for each control which do not need to retain the values during postback.

Note that ViewState is enable by default. You should disable it, if you do not need it.




Responses

Author: Shuby Arora    08 Feb 2009Member Level: Gold   Points : 2
Hi Tony,

A very good article I was dithering about the same concept, here are m points :


ViewState: Its nothing but it’s a hidden data which is kept by asp.net pages in “_VIEWSTATE” as hidden form field. They track the changes to a web site during post backs.

The ViewState of a page can be seen if you view the source code of the running page(Right Click --> View Source). It will be listed under the following tags:

Having a large ViewState will cause a page to download slowly from the users side. When a user clicks on a button and a post back occurs, all the view state information has to be posted back to the server which causes a slower request. Most times this is insignificant, but imagine a person using a dial up connection. If requests, and page loading is taking a couple of minutes, most likely the user will get frustrated, and leave.
Note:
1. By default, View State is enabled for every control in the ASP.NET Framework.
2. ViewState does not hold the controls, rather it holds the values of the form controls and their corresponding ID's that would otherwise be lost due to a post back because they do not post with the form.
3. ViewState is not used to hold session data or to transmit data between pages.
4. ViewState does not recreate the dynamically created controls of a page.
5. It does not restore the values to the controls after a post back operation.




Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
(No tags found.)

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: How to find the windows folder and other special folders.
Previous Resource: Secure Coding Guidelines for the .NET Framework
Return to Discussion Resource Index
Post New Resource
Category: .NET Framework


Post resources and earn money!
 
Related Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use