Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Building Controls > Failed to load viewstate

Reply
Thread Tools

Failed to load viewstate

 
 
Dan
Guest
Posts: n/a
 
      07-18-2003
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
 
Reply With Quote
 
 
 
 
Teemu Keiski
Guest
Posts: n/a
 
      07-18-2003
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



 
Reply With Quote
 
 
 
 
Dan Rush
Guest
Posts: n/a
 
      07-18-2003
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!
 
Reply With Quote
 
Teemu Keiski
Guest
Posts: n/a
 
      07-19-2003
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

> >
> >
> >.
> >



 
Reply With Quote
 
Dan Rush
Guest
Posts: n/a
 
      07-20-2003

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!
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Errors: Failed to load viewstate. & Validation of viewstate MAC failed. sck10 ASP .Net 6 09-01-2006 05:59 PM
Failed to load viewstate. The control tree into which viewstate... Josema ASP .Net Building Controls 2 05-22-2005 10:38 AM
Failed to load viewstate. The control tree into which viewstate... Josema ASP .Net Web Controls 0 05-09-2005 10:58 AM
Failed to load viewstate (i.e., I'm doomed). Mike Hnatt ASP .Net 2 11-03-2003 08:27 PM
Failed to load ViewState John Kirksey ASP .Net 5 07-22-2003 03:06 PM



Advertisments
 



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