Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / Building Controls / May 2005

Tip: Looking for answers? Try searching our database.

Failed to load viewstate. The control tree into which viewstate...

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Josema - 09 May 2005 11:58 GMT
Hi to all,

I have a webform.aspx and inside i have a dropdown list with autopostback
and a place holder...

Depending of the selected index that the user selects in the dropdownlist i
load a usercontrol into the place holder...

i dont know why im getting this 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.

and this is that i have:

if(!Page.IsPostBack)
{
   //fill the dropdownlist with the options
}
else
{
   switch(this.OpcionInicialDDL1.SelectedValue)
   {
     case "0": //Option 0 is "choose option" dont fill the placeholder
     break;
     case "1": //Option 1 is "Phone", add to place holder the phone
usercontrol
       
PhoneControl=(UserControls.Phone)LoadControl("UserControls/Phone.ascx");
//load the control phone
       this.OpcionPH.Controls.Add(PhoneControl);//add into the placeholder
controls the control phone
     break;
     case "2": //Option 2 is "Internet", add to place holder the Internet
usercontrol
   
InternetControl=(UserControls.Internet)LoadControl("UserControls/Internet.ascx"); //load the control internet
   this.OpcionPH.Controls.Add(InternetControl); //add into the placeholder
control the internet
break;
  case "3": //Option 3 is "Phone + Internet", add to place holderthe Phone
and Internet usercontrol
  PhoneControl=(UserControls.Phone)LoadControl("UserControls/Phone.ascx");
//load the control phone
 
InternetControl=(UserControls.Internet)LoadControl("UserControls/Internet.ascx"); //load control internet
 this.OpcionPH.Controls.Add(PhoneControl); //add into the place holder the
phone control
 this.OpcionPH.Controls.Add(InternetControl); //add into the place holder
the internet control
break;
 }
}

Any help would be appreciated.

Signature

Thanks
Regards.
Josema
--
Thanks
Regards.
Josema

sam - 18 May 2005 19:35 GMT
Hi there,

You can't do that.  Specifically, you can't change the controls around
like that.  You have to have the same controls added to the placeholder
each time.  I can explain why if you want.  Bascially it has to do with
how the framework restores Viewstate.

*Unless* you disable Viewstate.  After you load each control and right
before you Add() it try doing a EnableViewState=false.  That should fix
it but then you can't use Viewstate on those controls.
komil13 - 20 May 2005 19:25 GMT
Hi Sam,

Can you explain those details please?  Also, is there any way t
intercept viewstate after the controls have been added to the page an
before the ViewState kicks in?

I am trying to create a set of controls dynamically and based on wha
was selected more dynamic controls will be added.

Any help here will be appreciated.

Thank


sam - 22 May 2005 11:35 GMT
What you can do is have 5 (or however many user controls you can have
maximum) placeholders always on the page (you can hardcode these in the
aspx page if you want) and set the .Visible property of all these to
false.  Then load *all* the user controls into the placeholders on page
load.  Then, in the drop down list event handler (or later in the page
load, since you seem to be doing that) set the .Visible property of the
correct placeholder to true.  That will do the trick.  EnableViewstate
on everything.  Forget what I said before about making it false :).

By the way, you can indeed override OnLoadViewState but that won't stop
child controls from repopulating Viewstate.  The only way to stop it is
to set .EnableViewState = false on a parent control, as I mentioned
before.

I see you are using dropdownlists so you need Viewstate so that the
auto change event will be fired, as you have doubtless figured out by
now.  So my previous answer will be the only one that can work.  If you
need more help or I said something wrong just reply.

The reason you cant change the control tree is because the framework
uses the indexes into the control tree rather than the client ids to
figure out which control needs which Viewstate.  So it gets seriously
confused when the indexes say a control is supposed to be there and it
isn't because you've changed it.  So it throws that error.

-Sam Robertson

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.