Failed to load viewstate

Discussion in 'ASP .Net Building Controls' started by Dan, Jul 18, 2003.

  1. Dan

    Dan Guest

    I'm trying to replace my panels with usercontrols. On
    pageload, I pull an array and it's index from the session
    and I load a corresponding usercontrol and add it to a
    placeholder. This control dynamically gets webcontrols
    added to it. When user clicks next, I remove the control,
    get the next control in the array and add it to the
    placeholder. This works good. When user clicks back, I
    remove the current control, get the previous control and
    load it. During the pageload of this control i try to
    dynamically add webcontrols again and I get 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.

    I've also tried to set the existing control to invisible
    instead of removing it, but I still get this error.

    Help
    Dan, Jul 18, 2003
    #1
    1. Advertising

  2. Dan

    Teemu Keiski Guest

    Dynamical controls should be added on every request and on same order.
    ViewState saving and loading mechanism relies on control's index on control
    tree (it does not use IDs whatsoever) so therefore you can get this error if
    you modify the control tree improperly say put control A on index 2 on first
    request and on index 5 on next one and so on.

    Post some code which causes you the error and we can take a look at it.

    --
    Teemu Keiski
    MCP, Designer/Developer
    Mansoft tietotekniikka Oy
    http://www.mansoft.fi

    AspInsiders Member, www.aspinsiders.com
    ASP.NET Forums Moderator, www.asp.net
    AspAlliance Columnist, www.aspalliance.com


    "Dan" <> wrote in message
    news:0a4b01c34d55$0a0f7550$...
    > I'm trying to replace my panels with usercontrols. On
    > pageload, I pull an array and it's index from the session
    > and I load a corresponding usercontrol and add it to a
    > placeholder. This control dynamically gets webcontrols
    > added to it. When user clicks next, I remove the control,
    > get the next control in the array and add it to the
    > placeholder. This works good. When user clicks back, I
    > remove the current control, get the previous control and
    > load it. During the pageload of this control i try to
    > dynamically add webcontrols again and I get 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.
    >
    > I've also tried to set the existing control to invisible
    > instead of removing it, but I still get this error.
    >
    > Help
    Teemu Keiski, Jul 18, 2003
    #2
    1. Advertising

  3. Dan

    Dan Rush Guest

    Okay, Here is the web page page load method:

    private void Page_Load(object sender, System.EventArgs e)
    {
    myReq = (SupportRequest)Session["request"];
    if (myReq.options.Count > 0 && myReq.optionIndex <
    myReq.options.Count)
    {
    opt = (Option)myReq.options[myReq.optionIndex];
    opt.LoadPanel(this);
    }
    }

    Here is Option's LoadPanel method:

    public void LoadPanel(System.Web.UI.Page page)
    {
    // Load user control
    userControl = (UserControl)page.LoadControl ("Controls\\UserControl" +
    this.type + ".ascx");
    PlaceHolder plh = (PlaceHolder)page.FindControl("plhControls");
    plh.Controls.Add(userControl);
    }

    Here is Option's Next button action:

    private void btnNext_Click(object sender, System.EventArgs e)
    {
    myReq = (SupportRequest)Session["request"];
    Option opt = (Option)myReq.options[myReq.optionIndex];

    // Save current page and turn it off
    System.Web.UI.Page page = this.Page;
    opt.SavePanel(page); // this removes the usercontrol from a
    placeholder on the page using plh.Controls.RemoveAt(0)
    opt.Save();

    myReq.optionIndex++;

    if (myReq.optionIndex < myReq.options.Count)
    {
    opt = (Option)myReq.options[myReq.optionIndex];
    opt.LoadPanel(page);
    }
    }

    Here is a Back button action:

    private void btnSumBack_Click(object sender, System.EventArgs e)
    {
    SupportRequest myReq = (SupportRequest)Session["request"];
    System.Web.UI.Page page = this.Page;

    PlaceHolder plh = (PlaceHolder)page.FindControl("plhControls");
    plh.Controls.RemoveAt(0);

    myReq.optionIndex--;

    if (myReq.options.Count > 0)
    {
    opt = (Option)myReq.options[myReq.optionIndex];
    opt.LoadPanel(page);
    }
    }



    *** Sent via Developersdex http://www.developersdex.com ***
    Don't just participate in USENET...get rewarded for it!
    Dan Rush, Jul 18, 2003
    #3
  4. Dan

    Teemu Keiski Guest

    I know this sounds tedious, but if you replace control with others on
    control tree (between requests) you get problems as control do not get
    correct ViewState returned etc (as I said they use indexes to save/restore
    viewstate).

    Other thing is that you can't add control that does postback processing
    (Button or TextBox) on event handler itself. Reason is that those controls
    get the postback events raised right after Page_Load and if you add control
    on postback event, it won't get it's events raised on the proper request (on
    the first postback) but raises them on the second (like on postback behind
    all the time), as you explained the behaviour.

    I would suggest that you'd add all controls to the control tree and do not
    conditionalize the adding, but manage their visibility property. That way
    you'd get only desired controls visible/not visible, but wouldn't suffer any
    problems with postbacks whatsoever.

    --
    Teemu Keiski
    MCP, Designer/Developer
    Mansoft tietotekniikka Oy
    http://www.mansoft.fi

    AspInsiders Member, www.aspinsiders.com
    ASP.NET Forums Moderator, www.asp.net
    AspAlliance Columnist, www.aspalliance.com

    "Dan" <> wrote in message
    news:027c01c34d74$2941f780$...
    > Not getting this error anymore. I changed the next and
    > back button actions to remove the current control first,
    > then load the next control using plh.Controls.Remove(0)
    > and plh.Controls.Add(userControl).
    > The only problem I have now is the back button needs to be
    > clicked twice for each control. When I debug the first
    > back click, the button action never gets called, only the
    > pageload method. The second back click fires the action
    > and it works fine. Any ideas?
    > I tried sending code earlier, it hasn't showed up yet.
    > i can try again if you want.
    >
    > >-----Original Message-----
    > >Dynamical controls should be added on every request and

    > on same order.
    > >ViewState saving and loading mechanism relies on

    > control's index on control
    > >tree (it does not use IDs whatsoever) so therefore you

    > can get this error if
    > >you modify the control tree improperly say put control A

    > on index 2 on first
    > >request and on index 5 on next one and so on.
    > >
    > >Post some code which causes you the error and we can take

    > a look at it.
    > >
    > >--
    > >Teemu Keiski
    > >MCP, Designer/Developer
    > >Mansoft tietotekniikka Oy
    > >http://www.mansoft.fi
    > >
    > >AspInsiders Member, www.aspinsiders.com
    > >ASP.NET Forums Moderator, www.asp.net
    > >AspAlliance Columnist, www.aspalliance.com
    > >
    > >
    > >"Dan" <> wrote in message
    > >news:0a4b01c34d55$0a0f7550$...
    > >> I'm trying to replace my panels with usercontrols. On
    > >> pageload, I pull an array and it's index from the

    > session
    > >> and I load a corresponding usercontrol and add it to a
    > >> placeholder. This control dynamically gets webcontrols
    > >> added to it. When user clicks next, I remove the

    > control,
    > >> get the next control in the array and add it to the
    > >> placeholder. This works good. When user clicks back, I
    > >> remove the current control, get the previous control and
    > >> load it. During the pageload of this control i try to
    > >> dynamically add webcontrols again and I get 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.
    > >>
    > >> I've also tried to set the existing control to invisible
    > >> instead of removing it, but I still get this error.
    > >>
    > >> Help

    > >
    > >
    > >.
    > >
    Teemu Keiski, Jul 19, 2003
    #4
  5. Dan

    Dan Rush Guest

    concur about adding all controls and making one visible at at time to
    simulate multiple pages.

    So in my pageload method I do something like this?

    Page_load()
    {
    If not postback,
    {
    Loop through array, load all controls
    }

    Controls[index].Visible = true;

    }

    *** Sent via Developersdex http://www.developersdex.com ***
    Don't just participate in USENET...get rewarded for it!
    Dan Rush, Jul 20, 2003
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John Kirksey

    Failed to load ViewState

    John Kirksey, Jul 11, 2003, in forum: ASP .Net
    Replies:
    5
    Views:
    2,159
    John Kirksey
    Jul 22, 2003
  2. sck10
    Replies:
    6
    Views:
    909
    sck10
    Sep 1, 2006
  3. Josema
    Replies:
    2
    Views:
    345
  4. Josema
    Replies:
    0
    Views:
    204
    Josema
    May 9, 2005
  5. Albert-Jan Roskam
    Replies:
    2
    Views:
    79
    Chris Angelico
    Nov 25, 2013
Loading...

Share This Page