What is the purpose of CreateChildControls()?




I'm coding a custom server control, and CreateChildControls() is giving me
some pain. I'm just wondering why we have CreateChildControls(), and why
this methods seems to be called at different times in the page lifecycle. I
mean the order it is called is not deterministic. Example if not post back,
then my CreateChildControls() is being executed before my Load event. But
for post back, my Load event executes before my CreateChildControls().

I suppose calling EnsureChildControls() will call CreateChildControls() if
it has not already been called. But why all this complication? I'm even
thinking about creating my controls in the constructor instead of inside
CreateChildControls(); at least this way I know that my controls will have
been created when I need to use them.



Kevin Spencer

Hi Michael,

Here's a good reference for this:


At the bottom, there is a note:

"The CreateChildControls method is not listed in the table because it is
called whenever the ASP.NET page framework needs to create the controls tree
and this method call is not limited to a specific phase in a control's
lifecycle. For example, CreateChildControls can be invoked when loading a
page, during data binding, or during rendering."


Kevin Spencer
Microsoft MVP
Professional Chicken Salad Alchemist

A lifetime is made up of
Lots of short moments.


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