C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Communities   Interview   Jobs   Projects   Offshore Development    
Silverlight Tutorials | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Revenue Sharing |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...

New Feature: Community Sites: Create your own .NET community website and start earning from Google AdSense ! It's Free !




ViewState - behind the scenes


Posted Date: 30 Apr 2006    Resource Type: Articles    Category: .NET Framework

Posted By: Tony John       Member Level: Gold
Rating:     Points: 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


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search 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   BizTalk Adaptors    Web Design

Help Desk

Contact Us    Privacy Policy    Terms Of Use