Asp – Failed to load viewstate when dynamically loading different controls

asp.netviewstate

I have an aspx page which is loading some controls dynamically on postback (ie when a click event happens) and adding them to a placeholder. Depending on what is clicked a different set of controls needs to be loaded.
This works perfectly the first time, but when I clear the placeholder's controls and attempt to dynamically load a different set of controls 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."

This happens even if I do ViewState.Clear().

Is there a way to do this?

Best Answer

Yuriy Solodkyy explains it well here: Dynamically Created Controls in ASP.NET

Related Topic