hide the lines of VIEWSTATE in view source of a page

ANAT2403
ANAT2403 used Ask the Experts™
on
Hi,
In asp.net 2.0 with C# I want then when I will look at the running page in the browser and make view source I will not see the VIEWSTATE  word  appears at all.
I see  this situation in other sites. How this is done?
Anat
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
Just don't use VIEWSTATE unless you have to.

Author

Commented:
Hi,

I did not use it . It is created automatically and I don't know what cause it? This is what I want to understand.
Thankyou
anat
Commented:
Set
<%@ Page EnableViewState="false" %>

and then set EnableViewState=false for all controls on the page.
CompTIA Network+

Prepare for the CompTIA Network+ exam by learning how to troubleshoot, configure, and manage both wired and wireless networks.

Top Expert 2007
Commented:
You should read up on Viewstate before you disable it.
You don't use it directly, but ASP.Net uses it to track data between postbacks.
Disabling the viewstate can have serious repercussions.
And Viewstate is specific to ASP.Net, so if you are doing a "view source" on a page that isn't an aspx page, you won't see a viewstate hidden variable...but again...it's a pretty important thing to the way ASP.Net works.
Commented:
ViewState can be disabled at the application level through web.config file, at page level through Page directive and at control levels. It of course affects the speed of your application but the point is whether you require it or not? If you dont then it makes sense to disable it and the performance issues will reflect, but if you need to create an application where you have to show the data back to user after postback then why would you write extra code and reflect the data yourself? In that case ViewState should be enabled. There are a few points that you should keep in mind with ViewState:
1. If you enable it for a page, make sure to disabled it for controls like Label etc. which would not be requiring ViewState.
2. In case you can set the property of some control at design time then disable its ViewState.
3. In case you are filling for control like Grid, Listbox etc on every page load, disable its viewstate as in that case it wont make sense.

Author

Commented:
Hi,
I understand it now much better. Just one more question:
I don't use a gridview but I do use an asp: repeater control. and I have event in the databound of this.
How the repeater work with the viewstae if at all?
And also how the menu control and masterpage work or use the view state.
Thankyou
Anat
Top Expert 2007

Commented:
Viewstate is most important when you have input controls and do things that changes data and do things that require the page to "remember" values between postbacks.
If your repeater doesn't act as an "edit" form, then turning viewstate off should be ok.
It's not something I'd recommend be done globally (in web.config).
Easiest thing to do is disable it and test to see if you notice differences.

Commented:
Correct. If in doubt, simply enable/disable viewstate to notice the difference.

Author

Commented:
Hi everybody,
I made a test and discovered strange things:
I took my web site and and made 4 tests:
1. I put enableviewstate=false in one of the content pages.
2. I did 1 + enableviewstate=false  in master page
3. I put <pages enableViewState="false"  /> in web.config with and without 1+2
4. I put in each server control in a content page enableviewstate=false  
results:
in all tests when I looked at view source I see that
input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwUKLTI0NzcwMjk1......
was in a smaller size but still it remains big and did not disappear.
only test 1 did the job. all the other tests did not have any affect.
As I render my page in each loading I don't need the viewstate and I want to disabe it.
How is it done?

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial