1

I have a custom field with a TextBox, a Button, a ListBox, and another Button. When I click one of the buttons, I get the following error (there is currently no logic or data populating the textbox or listbox, I'm just trying to wire things up):

"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."

I am adding my controls every OnLoad. Any suggestions?

Andrew Barber
  • 39,603
  • 20
  • 94
  • 123
birdus
  • 7,062
  • 17
  • 59
  • 89

2 Answers2

1

Look at the ASP.net page life cycle. I think this error occurs when the control tree has changed from when the viewstate was generated, so it no longer matches i.e. controls cannot be repopulated from viewstate.

OnLoad is too late in the process to re-create controls. Perhaps you just need to move this to Init instead.

See other answers - Add dynamic controls in ASP.NET, is there a difference between 1.1 and 2.0?

Community
  • 1
  • 1
Paul George
  • 1,788
  • 16
  • 33
-1

I've experienced similar problems. Will you be needing the viewstate? Otherwise, try disabling it completely by setting enableviewstate="false" in your <%@ page %> declaration.

Mark Ursino
  • 31,209
  • 11
  • 51
  • 83
Andreas Bergström
  • 713
  • 1
  • 5
  • 18
  • I don't think I'll need viewstate, but this is a user control and I don't think I have access to the page declaration in the actual Sitecore page that will host the user control. – birdus Jan 13 '12 at 15:19
  • This is not needed to fix this issue. – Andrew Barber Jan 19 '12 at 01:40