Question for Gurus on CreateChildControls

  • Thread starter Thread starter Alex
  • Start date Start date
A

Alex

I believe that when you instantiate a Control derived custom control that
implements the virtual method CreateChildControls(), that that base class
constructor in Control calls EnsureChildControls() which will invoke
CreateChildControls(). Am I correct?
 
Alex said:
I believe that when you instantiate a Control derived custom control that
implements the virtual method CreateChildControls(), that that base class
constructor in Control calls EnsureChildControls() which will invoke
CreateChildControls(). Am I correct?

No. This is very incorrect. If this were the case, there would be no need
for a separate EnsureChildControls, since they would always be there.

A piece of code calls EnsureChildControls whenever it requires that the
child controls be present. If nothing else, ASP.NET will call
EnsureChildControls at the beginning of the PreRender phase.

A piece of code which is changing things in such a way as to cause a change
in the set of child controls should set "ChildControlsCreated = false" so
that the next time that EnsureChildControls is called, CreateChildControls
will be called.
 
No. This is very incorrect. If this were the case, there would be no
need for a separate EnsureChildControls, since they would always be
there.

A piece of code calls EnsureChildControls whenever it requires that
the child controls be present. If nothing else, ASP.NET will call
EnsureChildControls at the beginning of the PreRender phase.

A piece of code which is changing things in such a way as to cause a
change in the set of child controls should set "ChildControlsCreated
= false" so that the next time that EnsureChildControls is called,
CreateChildControls will be called.

Please explain me then, why after I create a custom control using
Type.CreateInstance(), CreateChildControls() is invoked before my
constructor? Every time. Do you know for sure that Control constructor does
not invoke EnsureChildControls()?
 
Alex said:
Please explain me then, why after I create a custom control using
Type.CreateInstance(), CreateChildControls() is invoked before my
constructor? Every time. Do you know for sure that Control constructor does
not invoke EnsureChildControls()?

Absolutely.

But why in the world are you creating controls using Type.CreateInstance???
That's sufficiently unusual that it might be the cause of your problem.
 
But why in the world are you creating controls using
Type.CreateInstance??? That's sufficiently unusual that it might be
the cause of your problem.

My architecture dictates a single-page(.aspx) with the content area being a
panel that can contain a different custom control depending on URL query
string. So I use query string parameters to form type name and use
Reflection API to create get the type and create instance. This is an
implementation of the Builder design pattern.
 
Alex said:
My architecture dictates a single-page(.aspx) with the content area being a
panel that can contain a different custom control depending on URL query
string. So I use query string parameters to form type name and use
Reflection API to create get the type and create instance. This is an
implementation of the Builder design pattern.

Interesting. In a similar situation, I've filled the panel with a calculated
user control. That allows "designer-type" people to create the content
visually. The same application also plays URL games so that users don't have
to see things like query strings.

I still don't know if the CreateInstance is your problem. As an experiment,
how about replacing your Type.CreateInstance with "new SomeControl()"? If
that works, but CreateInstance doesn't, it will tell you something.
 
Back
Top