0

I have two dynamic controls that are generated depending on a radio button list selection, which keep their values on postback, however, when I change to another control type I get an error on the viewstate tree because it is expecting anther object type. Is there a way I can delete the previous object from the viewstate when I change the type?

This is a simplified code example to make it easier to examine

if (rblFilter.SelectedValue == "1")
{
   var object1 = new object();
   holder.Controls.Add(object1);
}
else
{
   var object2 = new object();
   holder.Controls.Add(object2);
}

The error I get is the following

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

  • See if you can find your answer from this post. https://stackoverflow.com/questions/11795994/failed-to-load-viewstate-the-control-tree-into-which-viewstate-is-being-loaded , else you need to place actual code you are using. – yogesh keraliya Aug 20 '19 at 16:55
  • If you're using webforms, you have the IsPostBack to check the obvious, and the OnPreRender which will let you do stuff before the screen is drawn. I think you should just restore your viewstate and modify your control after. – nocturns2 Aug 20 '19 at 16:58
  • @yogeshkeraliya that is exactelly the code I am using, I just removed what wasn't important. I already checked that post but I don't want do disable the viewstate – João Pedro Sousa Aug 20 '19 at 17:02

0 Answers0