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.