?

Log in

No account? Create an account
ASP.Net view states - Nick [entries|archive|friends|userinfo]
Nick

[ website | gagravarr.org ]
[ userinfo | livejournal userinfo ]
[ archive | journal archive ]

ASP.Net view states [Feb. 16th, 2004|05:37 pm]
Nick
Can anyone explain to me what (if anything other than "how the hell can we shoe horn this stuff in so it doesn't immediately fall over...") Microsoft were thinking when they dreamed up the view state rubbish in ASP.Net?

With MCMS, you have weird dual personality web controls (known as placeholder controls) on your page - in one mode they let you enter content, in another they display it. Sounds quite nifty, until you start writing your own. Around the point your controls start doing anything interesting, you hits errors like:

Server Error in 'your-application' Application

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.

Description: An unhandled exception occurred during the execution of the current web 
request. Please review the stack trace for more information


At this point, you enable debugging of the ASP.Net process in IIS (one of the very few stunningly useful things in ASP.Net with VS), and take a peek. Nothing looks wrong. So, you cut and paste the error into google, and stir up a hornets nest.

Annoyingly, nothing there dealt with the MCMS case, but there was lots of somewhere related information. Much of it dealt with how random and unhelpful this error is. Most of it talked about all the gotcha's associated with how ASP.Net decides to load up and render your page, especially when being called to handle a web event (normally someone submitting a form). It really is an icky mess. Around the time people start suggesting subclassing everything in sight and adding tracing just to see how and where components are added to the page and filled with content, you know you're not in Kansas any more...

Since I really don't intend to be doing anything like what the pages describing viewstate and its errors were talking about (too many buttons with too much intelligence in each one on a page, with callback handlers to them), I've found a simple solution:
protected override void LoadViewState(object state) {
	// Do nothing. You'll only end up crying when you get bizare errors
}
protected override object SaveViewState() {
	// Do nothing. See above
	return null;
}

Lo, my control behaves itself. Now back to coding, having just wasted much of the afternoon....
linkReply

Comments:
From: (Anonymous)
2004-04-21 09:31 pm (UTC)

Fail to Load ViewState while add the control dynamically

The solution provided is still giving the problem. Actually There are lot of control on my Page and i just want to change the positions of the controls at run time. Can any body can help. Sanjeev Azad
(Reply) (Thread)
From: gagravarr
2004-04-26 06:04 am (UTC)

Re: Fail to Load ViewState while add the control dynamically

I can only suggest you rip out controls until it starts working, then slowly add them back in. You could also try to ensure that the view state is completely blank before beginning.

The whole view state cache thingy is a bit hacky, and a lot flakey, so you'll have to put up with issues like this.
(Reply) (Parent) (Thread)