Home
 Index > ASP.NET Web Applications > Failed to load viewstate. The control tree int...

ASP.NET Web Applications:  Debugging , FormView

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.

Error continues to state: 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.

Added on 12 Jul 2008

Scenarios:

Scenario Summary:
Occurs after the cancel button has been clicked in the FormView's ItemTemplate and the ItemTemplate is re-bound. Does not occur for the EditItemTemplate's cancel button.
Scenario Details:

Most of the advice in .NET forums suggest exactly what the error message says -- namely, that something went wrong when adding controls dynamically.  However, this message can occur in, what seems to me, completely different scenarios.  I imagine many more scenarios will be added in the future for this error message, but I'll start with this simple one:

  • We have a FormView and a Repeater (outside of the FormView)
  • A repeater item's linkbutton is clicked and we load the details into the FormView's ReadOnly Mode (i.e., using the ItemTemplate)
  • We click the Cancel button in the ItemTemplate.
  • We click another LinkButton in one of the Repeater Items
  • The error is thrown even though no controls were added dynamically!
  • When clicking the Cancel button in the EditItemTemplate (Update mode), no errors are thrown. 
Added on 12 Jul 2008

Solution Summary:
Don't use "Cancel" as a CommandName in the ItemTemplate.
Solution Details:

The CommandNames "Delete", "Update", "Insert" and "Cancel" seem to be reserved for EditItemTemplates and InsertItemTemplates.  If you have a cancel button in the ItemTemplate (Readonly), ensure you give it a unique CommandName.

Changing the CommandName for the Cancel Button in the ItemTemplate to "_Cancel" and leaving the EditItemTemplate Cancel button's CommandName as "Cancel", worked for me.  It simply meant that I needed to add another case value to my ItemCommand event routine:

    Private Sub fv_Member_ItemCommand(ByVal sender As ObjectByVal e As System.Web.UI.WebControls.FormViewCommandEventArgs) Handles fv_Member.ItemCommand
        
Select Case e.CommandName
            
Case "Cancel""_Cancel"
                Me.Change_Mode(DisplayMode.m_Default)
            
Case "DoSomethingelse"
                ' do something else
        End Select
    End Sub

Was this solution useful? Yes No Added on 12 Jul 2008
Rating: 

Copyright 2010 © E-Centric, Inc. | Terms of Use