Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Minimizing viewstate- confused by `EnableViewState` and `ViewStateMode` in asp.net 4.0

I'm trying to clean up an older ASP.NET WebForms site that has ViewState enabled everywhere. This is a performance issue - huge viewstates cause noticeable submit delays. But most of the forms don't really seem to need ViewState except for some complex control & form data. Even forms with no input controls, though, generate big viewstates because I guess asp.net is storing all kinds of metatdata about every single server control. But the visibility state, etc, is all controlled in code so I think I can eliminate a lot.

It's pretty onerous to add EnableViewState="false" to every single control (in a page and created in code) that doesn't need it, so I'm trying to disable it at a page/control level, and selectively enable it for things that need it. (Yes, I realize this is risky, but there are really just a couple big forms and a couple templates that, if addressed would make a big difference).

Here's what I'm not quite getting.

If a control or page has EnableViewState="false" it its <%.. %> descriptor, or in its tag where it is created in the parent page, everything breaks, because any ViewState data added in code doesn't work. So I seem to be able to leave it enabled at the control level, but set EnableViewState to false for a wrapper control in each container, and then set ViewStateMode=true (which ovverrides that) at a per-control level.

What I'm not getting is what happens when:

ViewStateMode = ViewStateMode.Enabled and EnableViewState = false for a control that contains other controls.

for a control. Can inner controls be enabled still with ViewStateMode? Basically, which setting has the final word when they conflict?

For each container, I want to be able to disable everything in a wrapper control but still ensure that:

1) ViewState settings in code work, and

2) ViewState is disabled for all controls by default, and

3) I can selectively enable ViewState for subcontrols.

This seems to be confounding. If I have a wrapper control in the master page that is set to EnableViewState="false", but then set a subcontrol to ViewStateMode="Enabled", it breaks. According to MS, ViewStateMode should supercede any outer ViewState settings, yet it doesn't seem to work.

like image 809
Jamie Treworgy Avatar asked Mar 30 '11 11:03

Jamie Treworgy


1 Answers

As per the MSDN article on ViewStateMode, ViewStateMode only has meaning when EnableViewState="true". To achieve what you want, you'll have to leave ViewStateMode="Enabled" on the control, and then wrap the sub-controls of the main one in an asp:Placeholder that has ViewStateMode="Disabled". That way, you can still manipulate ViewState in the codebehind, but no child control will have ViewState (except the ones you explicitly set to have it via ViewStateMode="Enabled").

like image 196
Dan Davies Brackett Avatar answered Oct 17 '22 02:10

Dan Davies Brackett