tags:

views:

388

answers:

4

we know that view state is easily get abused, but asp.net webform are heavily depending on this feature.

I want to know whether you disable viewstate by default, and ONLY add it wehn it's needed. Or you take the visual studio default, which actually enables viewstate by default.

+3  A: 

I tend to take the default of having it on when doing web.forms.

But when writing my own user controls I disable it until I need it, or I find something doesn't work. I also monitor the size of the view state and when/if it gets too big I look at what the page is doing and change what I'm doing on the page. I.e. bind to data objects containing only the data that is needed and no more... stuff like that.

David McEwing
+1  A: 

Personnaly, no. Eventually, to have it disabled will be the behaviour that you will want but most of the time, no. Also, I would do a manual ViewState optimization pass and disabled controls' ViewState that wouldn't need it if really necessary. It's better not to have the ViewState to worry about while you are in heavy development imo. I'm not saying here that you shouldn't care about the ViewState at all but to put EnableViewState aside until you feel the need to lighten the trips to the server.

Maxime
A: 

I go one step further, I've created a class that inherits from the Page class and override 2 functions

protected override void SavePageStateToPersistenceMedium(object viewState) 
{
}

protected override object LoadPageStateFromPersistenceMedium()
{
 return null;
}

Then have my page that requires the viewstate to be disabled inherit from this. Works very well.

TWith2Sugars
+2  A: 

As others have said, unless we are talking about a fairly static page, I tend to leave the ViewState on while developing, and begin selectively disabling it when the page works. That way, it's one less thing to worry about upfront.

You may find this interesting, in ASP.NET 4.0, we will have more efficient control over disabling the ViewState: http://www.asp.net/learn/whitepapers/aspnet40/#_Toc223325478

Loren
Oh man, I want this so much.
Bill Ayakatubby