title: How do I decide what to store in viewstate?
body: <p>I have been writing a bunch of generic ASP.NET controls, and one thing I can't seem to wrap my mind around is when to store values in viewstate, and when to assume it's OK not to.</p>
  
  <p>On one hand, it makes sense to store the entire state of the control in viewstate, including properties like:</p>
  
  <ul>
  <li>Text box values entered by the user (or any form data)</li>
  <li>Configuration options like height or page size</li>
  <li>Even how the control has been composed - for example storing all the data a grid view is built from, or the grid itself.</li>
  </ul>
  
  <p>Ignoring performance, the more you can shove in viewstate the better, because it means the control will behave exactly the same across postbacks and never "accidentally" revert a value or "forget" it was disabled. But viewstate is not free. Storing everything means the control will now output both the HTML and all its internal properties to create that HTML, which would almost always more than double the output.</p>
  
  <p>My question is not about performance, but about strategy. <strong>On what criteria do I decide to put a property in viewstate?</strong> I was thinking something along these lines:</p>
  
  <blockquote>
    <p>If the user can't change a property, then the server will always set it explicitly, so it's OK to leave it out of viewstate. Even for something like <code>color=red</code>, the user doesn't set this property directly; they will click a button elsewhere which indirectly sets this property. That button or its owner should keep the state, not the control that renders the color red.</p>
  </blockquote>
  
  <p>This logic implies that the only properties that should go into viewstate would be:</p>
  
  <ol>
  <li>Form elements like <code>&lt;input&gt;</code> (and with <code>Request.Form[c.UniqueID]</code> this can be avoided still)</li>
  <li>Properties that the user can control interactively directly on the control.</li>
  </ol>
  
  <p>Does this logic make sense? It seems weak and I'd like to hear more from experts.</p>