title: Minimizing viewstate- confused by `EnableViewState` and `ViewStateMode` in asp.net 4.0
body: <p>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 &amp; 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.</p>
  
  <p>It's pretty onerous to add <code>EnableViewState="false"</code> 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).</p>
  
  <p>Here's what I'm not quite getting.</p>
  
  <p>If a control or page has <code>EnableViewState="false"</code> it its <code>&lt;%.. %&gt;</code> 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 <code>EnableViewState</code> to false for a wrapper control in each container, and then set <code>ViewStateMode=true</code> (which ovverrides that) at a per-control level.</p>
  
  <p>What I'm not getting is what happens when:</p>
  
  <p><code>ViewStateMode = ViewStateMode.Enabled</code> and
  <code>EnableViewState = false</code> for a control that contains other controls.</p>
  
  <p>for a control. Can inner controls be enabled still with <code>ViewStateMode</code>?  Basically, which setting has the final word when they conflict? </p>
  
  <p>For each container, I want to be able to disable everything in a wrapper control but still ensure that:</p>
  
  <p>1) <strong>ViewState settings in code work</strong>, and</p>
  
  <p>2) <strong>ViewState is disabled for all controls by default</strong>, and</p>
  
  <p>3) <strong>I can selectively enable ViewState for subcontrols.</strong></p>
  
  <p>This seems to be confounding. If I have a wrapper control in the master page that is set to <code>EnableViewState="false"</code>, but then set a subcontrol to <code>ViewStateMode="Enabled"</code>, it breaks. According to MS, ViewStateMode should supercede any outer ViewState settings, yet it doesn't seem to work.</p>