Fog Creek Software
Discussion Board

Welcome! and rules

Joel on Software

ASP.NET ViewState and dynamic controls quirks

This time I am not complaining about C#, ASP.NET, the .Net Framework or anything. I want to share one possible solution to a problem I encountered the other day.

Here's the scenario: I want a wizard-like screens to add and edit persons (not in the actual project, but managing persons is an easy sample).

The first screen can add a person or select one from a list to edit. Both actions navigates to the edit screen. The edit screen can edit the person's properties and update or go back.

I want to use a variation of the Model-View-Controller pattern, using the Page as the controller and the UserControls as the views. The Page will decide the active view and add its control to a PlaceHolder.

Adding the first control works fine, adding (navigation to) the second works, but when I navigate back to the first ASP.NET fails with an "Specified cast is not valid" exception.

It turns out the exception happens when the ViewState is restored. One possibility is to disable viewstate on each added control, an other is to always add all controls in the same order, then hide all but the active.

The penalty about the latter is that Page_Load is executed for all controls. But that can be avoided by moving the code to OnPreRender or, as I did, create a public Setup method and do the usual Page_Load stuff there. Setup will only be called on the active control, of course.

Thomas Eyde
Saturday, October 04, 2003

Looks like the abstraction is a-leakin'. Use the web paradigm: use pages are views, pass parameters as query string params. Then things like the back button etc work as expected.

Duncan Smart
Saturday, October 04, 2003

I disagree. The control model in ASP.NET is very, very powerful and flexible. It does come with a pretty high learning curve, though, when you work outside the drag and drop framework.

Rumor is that it's being overhauled in 2.0, hopefully for the better.

Brad Wilson (
Saturday, October 04, 2003

I must have missed the bit where I said that the ASP.NET control model was broken... I just thnk that expecting to program web apps as if they were stateful windows apps will always lead to frustration with the abstraction leaking.

Duncan Smart
Monday, October 06, 2003

This is semi-well documented.  A good starting point to see one way around this problem (and, to me it is a REAL problem) is the IBuySpy portal. 

In short what you want to do is instantiate the current (i.e., old) view during the Init() processing.  By the time one gets to PageLoad() the RestoreViewState() has already occured and you're too late.... so if you want dynamic views you need to add them early in the request/response cycle. 

Also, depending on if you're using the nifty .htc files (e.g., the IEWebControls, etc.) then you'll want to add the <%@ Reference %> or <%@ Register %> tags. 

Ezra Epstein
Tuesday, October 21, 2003

*  Recent Topics

*  Fog Creek Home