views:

166

answers:

2

I have an aspx page that dynamically loads user controls: there is an UpdatePanel that holds these controls (one each time). When I change a control with another one it fails with the message:

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 application. For example, when adding controls dynamically, the controls added during a return must match the type and position of the controls added during the initial application.

All of three controls inherit from System.Web.UI.UserControl. Maybe I can solve the problem doing:

  1. Load three controls at start time and then hide two of them as we need it.
  2. Use three UpdatePanels, one for each user control.
  3. Any advice?
A: 

Make sure that postbacks aren't updating undesired panels by setting the postback mode to conditional and updating them manually.

hypoxide
No, I've done it and it is throwing the same exception.
VansFannel
Try putting a breakpoint in your function that you'll hit when you post back. Then use the watch window to start prodding around in the .Controls list that contains your dynamically created controls. See if your controls exist in that list. If not, you'll have to add each control to its own update panel.
hypoxide
Now I use one UpdatePanel for each control. The problem has been solved, but now I have another one: Runtime Error in Microsoft JScript: Sys.InvalidOperationException: Could not find UpdatePanel with ID. 'CreateDestination1_DestinationMainUpdatePanel'. If you are upgrading dynamically, it must be inside another UpdatePanel.
VansFannel
I've found the solution here: http://forums.asp.net/p/1174373/1973874.aspx
VansFannel
A: 

When you dynamically add the user controls, are you assigning the id property? And are you adding the control again, on postback, setting the id property to the same value?

Jeff Siver