title: Problem with LoadViewState after F5 page refresh - Failed to load viewstate
body: <p>I'm having a problem in my ASP.NET application with Viewstate and F5 page refresh.</p>
  
  <p>If after navigating from one page to another I immediately hit F5, I get the following error:</p>
  
  <pre><code>System.InvalidCastException 
  "Unable to cast object of type 'System.Web.UI.Triplet' 
  to type 'System.Web.UI.Pair'."
  </code></pre>
  
  <p>This leads to the following exception displayed on the page:</p>
  
  <pre><code>HttpException (0x80004005): Failed to load viewstate.  The control tree 
  into which viewstate is being loaded must match the control tree that 
  was used to save viewstate during the previous request.  
  For example, when adding controls dynamically, the controls added during a 
  post-back must match the type and position of the controls added during 
  the initial request.]
  System.Web.UI.Control.LoadViewStateRecursive(Object savedState) +310
  System.Web.UI.Control.LoadChildViewStateByIndex(ArrayList childState) +136
  System.Web.UI.Control.LoadViewStateRecursive(Object savedState) +224
  System.Web.UI.Control.LoadChildViewStateByIndex(ArrayList childState) +136
  System.Web.UI.Control.LoadViewStateRecursive(Object savedState) +224
  System.Web.UI.Control.LoadChildViewStateByIndex(ArrayList childState) +136
  System.Web.UI.Control.LoadViewStateRecursive(Object savedState) +224
  System.Web.UI.Control.LoadChildViewStateByIndex(ArrayList childState) +136
  System.Web.UI.Control.LoadViewStateRecursive(Object savedState) +224
  System.Web.UI.Control.LoadChildViewStateByIndex(ArrayList childState) +136
  System.Web.UI.Control.LoadViewStateRecursive(Object savedState) +224
  System.Web.UI.Page.LoadAllState() +439
  System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1092
  </code></pre>
  
  <p>The point at which it fails is in the overriden LoadViewState method for a custom label control, when it calls the parent LoadViewState </p>
  
  <pre><code>System.Web.UI.WebControls.Label.LoadViewState(ByVal savedState As Object)
  </code></pre>
  
  <p>The savedState object appears to be for a completely different control (a drop down list) which is a System.Web.UI.Triplet, instead of the System.UI.Pair object expected by the Label control.</p>
  
  <p>If I do the exactly the same thing using a different page in the same application, the page loads fine after the F5 postback. The two web pages inherit from the same base class and use the same custom controls.  The page that is causing problems doesn't have any dynamically added controls.</p>
  
  <p>Having debugged the process, it looks like the LoadViewState event is not being fired after the F5 postback for the controls on the page that loads successfully. SaveViewState is fired in both cases.</p>
  
  <p>Any ideas why the LoadViewState event would be fired for one page but not the other? I'm fairly new to ASP.NET and I'm still getting my head around how ViewState works.</p>