Take the 2-minute tour ×

The Scenario

I'm working on a large web-forms-heavy intranet site right now, and we're trying to replace the 500+ forms with a generic, database-driven form engine which will dynamically add various fields and validation and wire up any drop-down bindings and so forth. So far, so good.

How we do this is fairly easy; we have a routine called during page init which gets the appropriate field definitions and associated validators (both client and custom) and basically adds each in turn to a placeholder.

As part of the remit we need to create some composite controls for things like 'platform' which have some specific server-side functionality that we can't really make generic, but which are used all over the place.

The plan was to simply extend the current design to add instances of these composite controls where required into the placeholder.

However, these controls do certain things on Load and PreRender which is where we have hit a problem.

The Problem

Recall that we're doing all of this in Init? Well, we need to in order to build in things like dynamic server-side custom validators (whose method names we hold in the database and basically wire-up using reflection after the associated control has been instantiated).

We are quite able to add instances of our composite control this way, and all appears well. However, when the control's Load or PreRender events are fired, we find a peculiar thing (and this is where I need you clever people to help me out).

Basically, I'm getting a NullReferenceException when I want to access any of the child controls on this composite control. These are simple things like an image that I want to hide (which is declarative on the .ascx and not dynamic) and a drop-down list (into which I'd like to stuff some values from the database). The strange thing is that even if I move this code out to PreRender, these controls don't appear to be instantiated even though they should be given that they're declared, um, declaratively and we're at the PreRender stage.

Here's a typical fragment (apologies, it's in VB.NET 2.0 due to client restrictions- that's just how it is ;-):

If Not IsPostBack Then

The exception occurs when we attempt to do something with any actual child control, such as the IsRequiredFieldIndicator control (which is an image). In other words, it ain't there. Oddly enough, when we inspect what's happening, the ChildControlsCreated property has been set to true yet the contents of the Controls collection shows nothing (empty).

Is there something fundamental that I'm missing here? The composite control works perfectly in all regards if we place it declaratively on the page, so I am tempted to say that the problem might be related to some lesser-known characteristic of the page life-cycle as regards dynamically added composite controls.

Oh, and before someone suggests it, I have ensured that the composite control implements the INamingContainer interface but to no avail.

Has anyone ever encountered something like this and, if so, how did you get around it?

Many thanks,


share|improve this question
add comment

1 Answer

OK, I figured it out. It's quite simple (isn't it always?). Basically, the answer lies in the fact that you simply can't just create a new instance of your user control dynamically and expect it to 'just work'. You need to use LoadControl() (which, of course, I knew, but hey, totally missed...). Otherwise, instantiating the control simply by using new() and then plugging values in misses the crucial step in which the .ascx declarative controls are 'folded' into the actual control setup... if that makes any sense. By using new() everything appeared to work but of course the control had missed this important step which was what was coming back to bite me..... d'oh!

share|improve this answer
Congrats on the solution. When you are able, please make sure to mark your answer as 'accepted' so that others might learn from your success. Cheers~ –  Andrew Kozak Dec 22 '11 at 2:17
add comment

Your Answer


