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

Ask a Question related to ASP.NET Building Controls, Design and Development.

  1. #1

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

    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("User Controls/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("User Controls/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.

    --
    Thanks
    Regards.
    Josema
    --
    Thanks
    Regards.
    Josema
    Josema Guest

  2. Similar Questions and Discussions

    1. Failed to load viewstate
      I have a page that uses an in-place editable DataGrid that supports sorting and paging. EnableViewState is turned ON. At the top of the page are...
    2. CreateChildControls and Exception Failed to Load ViewState
      Hi I've been struggling with this problem for several days now, pretty much out of ideas... I have n number of xml files each defines what...
    3. Failed to load viewstate on editable datagrid
      I am trying to add a new row to the header of an edittable datagrid. I have achieved this by adding a row using: ...
    4. Viewstate errors... how do I get viewstate working?
      Hi all, Have to say I can't for the life of me get this viewstate business working with collections of classes! The current error I'm getting...
    5. Page and Control ViewState
      I'm not sure if I'm missing something here, or not approaching this the right way, but here is my problem. I have a page Page.aspx that contains...
  3. #2

    Default Re: Failed to load viewstate. The control tree into which viewstate...

    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.

    sam Guest

  4. #3

    Default Re: Failed to load viewstate. The control tree into which viewstate...


    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


    -
    komil1
    -----------------------------------------------------------------------
    Posted via [url]http://www.codecomments.co[/url]
    -----------------------------------------------------------------------

    komil13 Guest

  5. #4

    Default Re: Failed to load viewstate. The control tree into which viewstate...

    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

    sam Guest

  6. #5

    Default Re: Failed to load viewstate. The control tree into which viewstate...

    Most of the advice in .NET forums suggest exactly what the error message says -- namely, that something went wrong when adding controls dynamically. However, this message can occur in, what seems to me, completely different scenarios. I imagine many more scenarios will be added in the future for this error message, but I'll start with this simple one:

    We have a FormView and a Repeater (outside of the FormView)
    A repeater item's linkbutton is clicked and we load the details into the FormView's ReadOnly Mode (i.e., using the ItemTemplate)
    We click the Cancel button in the ItemTemplate.
    We click another LinkButton in one of the Repeater Items
    The error is thrown even though no controls were added dynamically!
    When clicking the Cancel button in the EditItemTemplate (Update mode), no errors are thrown.

    Answer: you are the best:
    I did set the viewstate=false in item command under delete:
    Then in protected void dgCatDetails_RowDeleting(object sender, GridViewDeleteEventArgs e)
    before populateing the grid again i set the viewstate to true and populated the grid.
    Its works absolutely fine.
    Thanks I drove this conclusion based on your explanation.
    Jayanth Guest

  7. #6

    Default Re: Failed to load viewstate. The control tree into which viewstate...

    Jayanth, even I'm facing the same issue as yours. Please tell me how did you set the ViewState as false in delete command. Viewstate asks for a key - ViewState[key]. What key has to be put there?

    Please help. Its urgent.

    Thanks in advance.
    Unregistered Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139