When I disable ViewState for the page. It does not allow any other control to use ViewState .. even if I set EnableViewState="true" for that particular control ..
is it possible to enable ViewState for a control when ViewState is disabled for the page itself?
if not how can disable viewstate for controls on page except for few without specifying EnableViewState="false" explicitly .. typing the same into so many controls is hectic ..
With .NET 4.0 you can also define the Control Adapters
In above code, the ViewState mode is disabled when it is inherited. Add the following line in Browser settings file.
This way you can have more granular cotnrol over viewstate for each single control throughuout the application. And where you need control to have viewstate just enable it in code. That's it.
The simplest solution is:
and wherever you want to enable viewstate:
All credits reserved to Shredder's answer on this post
You could also inherit from a BasePage. On the BasePage disable ViewState.
In each page that you want ViewState Enabled, you do the following in Page_Load:
This should enable ViewState just for that Page (assuming it's ON in the MasterPage). Note: You will need to individually set each control's ViewState on that Page.
I'm curious to see if Samuel's approach works. If you try it please post your result.
I'm not saying Samuel's wrong, I'd just be curious.
The reason I'm curious is because since viewstate is serialized recursively (as Samuel mentioned) if you had one control with viewstate enabled that was a child of a control with viewstate disabled, then the child control wouldn't have viewstate because the recursive serialization would skip over it entirely at the parent level. This would specifically be troubling if you have built your own user controls that would naturally contain a lot of child controls.
Another solution would be to use Samuel's utility method approach but instead of disabling everything, just disable it for controls like Label, Literal, etc that do not have children...or if they do have children it's ok if the children have viewstate disabled.
You would naturally want to avoid disabling the viewstate of Panels and Placeholders for the reason I stated above.
Edit:
If you set turn page's ViewState off, then there is no way for you to enable ViewState for specific components. This is because ViewState is serialzed recursively, so when if the Page is not allowing ViewState, it will not serialize the ViewState for any of it's child controls.
In answer to your question, if you don't want to explicitly turn ViewState off on individual controls, but want to keep some controls ViewState aware, the best way would be writing a small utility method which turns ViewState off for all controls (using recursion or otherwise). Then enable ViewState for the controls that you would like to enable ViewState for.
Alternatively, a middle ground and less forceful way may possible if controls are groups inside other container controls (such as Panel). You can disable ViewState for all controls inside a Panel by disabling ViewState of the Panel.
Here's some code which expands on @Samuel Kim's concept of having a way to disable ViewState on all but certain controls (btw, it uses .NET 3.5):
The only thing I'm not 100% sure on (and I don't have my VM running) is whether Page.Controls needs to be cast or not, if so just have this instead:
The above is only a quick concept of what to do, it doesn't take into account nested controls where you may want 1 with and 1 without ViewState, but it wouldn't be hard to make a recusive function to handle it.