2

I am trying to design a page that handles both Employee and Station CRUD tasks depending on the user's preference. I have sub-classed UserControl to create an EditEntityControl and developed two custom controls that derive from this base class and each handle the CRUD activities for either the Employee or Station object.

When the user toggles the value in a dropdownlist, which triggers a postback, I want to dynamically load the correct control into the page. This works on the first load of each control, but when I try to reload the first control (after loading the second), I get the following error:

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 also see some strange behavior on the initial load of the second control where data bindings don't bind to the correct controls (setting the text of a button rather than a textbox on the control for example).

Is there a way to handle this scenario? Or, is there a way to clear out the ViewState and just re-request the page entirely to get around this error? It appears that if I could clear up this ViewState clutter/confusion between PostBacks, everything else is working as designed.

Michael Kingsmill
  • 1,815
  • 3
  • 22
  • 31
  • If you don't need ViewState, have you tried settng `EnableViewState` to false on the control, or the base control? – James Johnson Apr 16 '12 at 14:01
  • I have, however, I do need the `ViewState` on some PostBacks. These are input forms, so if the user clicks submit on one of the forms, I need the `ViewState` in that instance to persist the values to the database. I just want to clear out the `ViewState` in the instance where the second input control is requested. – Michael Kingsmill Apr 16 '12 at 14:05
  • 2
    First, it's lways better to just switch the visibility of UserControls if the number is predictable. Unvisible controls are not rendered at all, so performance is not an argument. Apart from that, you should show us where(in the page's life-cycle) and how you're loading these controls. Note that you can add controls dynamically after page_load, but you cannot **recreate** controls afterwards(f.e. in an event-andler). Have you assigned static IDs to them? How is it created? – Tim Schmelter Apr 16 '12 at 14:28

1 Answers1

1

Great suggestions in the comments above, but what finally pointed me to the correct solution was Joel Coehoorn's answer on this question.

I load up the control OnInit, then in the SelectionChanged event blow away the OnInit changes and recreate the correct control as needed. Thanks for the additional suggestions about making unused controls invisible. I will keep that in mind for future reference.

Community
  • 1
  • 1
Michael Kingsmill
  • 1,815
  • 3
  • 22
  • 31