Question for Gurus on CreateChildControls

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?
 
J

John Saunders

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.
 
A

Alex

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()?
 
J

John Saunders

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.
 
A

Alex

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.
 
J

John Saunders

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top