title: Why is ViewState lost when a control's property is modified during its parent's Control.Render method?
body: <p>I have code like the following in a UserControl:</p>
  
  <pre><code>Protected Overrides Sub Render(ByVal writer As System.Web.UI.HtmlTextWriter)
      If someCondition Then
          childControl.Enabled = false
      End If
  
      MyBase.Render(writer)
  End Sub
  </code></pre>
  
  <p>Whenever someCondition is true and childControl.Enabled is set to false during the Render event, the ViewState for childControl is destroyed (i.e. if it is a TextBox, the text that the user has entered is lost).</p>
  
  <p>Note that only the ViewState is lost...  The control still renders with the correct property values the first time around.  Only on PostBack, when properties are restored from ViewState are the values actually lost.</p>
  
  <p>The timeline is as follows:</p>
  
  <ul>
  <li>Page_Load (initial)<br>
  Properties are set via code.</li>
  <li>SaveViewState</li>
  <li>Render<br>
  Properties are modified.</li>
  <li>Postback occurs.</li>
  <li>LoadViewState</li>
  <li>Page_Load<br>
  Values of unaltered controls are still available, but controls which have had properties set during the Render method are blank.</li>
  <li>SaveViewState</li>
  </ul>
  
  <p>My understanding was that the ViewState became fixed during the call to Control.SaveViewState, which occurs prior to the call to Control.Render...  But is there some nuance that I'm missing?</p>