I've often wondered the same - and there are some people who don't bother
with CreateChildControls at all - see
http://www.wilsondotnet.com/Tips/Vie...spx?Thread=278 .
Possibly its only useful in the case that the child controls change as a
result of the postback (e.g. the expanding of a node in a simple serverside
treeview control) - then, you can just set ChildControlsCreated = false to
get them recreated (the next time EnsureChildControls is called.).
I can't quite see at first glance why "this is especially important when you
create composite controls whose child controls handle postback data.".
Presumably, to get viewstate loaded, and postback data also, you would need
to make sure EnsureChildControls was called in OnInit anyway - so why can't
you just create them in OnInit.
Maybe its more of a mini design pattern type thing - i.e., perhaps the
ASP.NET designers considered a control whose child controls remain static
(i.e. the actual objects in the Controls collection don't need to change
based on some postback event or value) as a special case, and the more
general case is that the Controls collection could vary on each postback. So
they might have thought this is best handled by a separate function
CreateChildControls, rather than coding directly in OnInit, and so tried to
enforce this as a best practice.
Its a very interesting question!
Cheers,
Pete Beech
"dx" <> wrote in message
news

6ednZjxk_jtIujdRVn-...
> I have the Microsoft Press: Developing Microsoft ASP.NET Server Controls
and
> Components book. It's starting to shine some light on control development
> but there is something about composite controls that I don't understand...
> I've included a snippet from Chapter 12 below on Composite Controls:
>
> <start>
>
> Override the CreateChildControls method to instantiate child controls,
> initialize them, and add them to the control tree. Do not perform this
logic
> in the constructor or in the OnInit method.
>
> Let's examine the reason for this implementation detail. You must create
> your child controls in the CreateChildControls method--instead of creating
> them in a specific phase such as Instantiate or Initialize--so that
children
> can be created on demand when needed in your control's life cycle. This is
> especially important when you create composite controls whose child
controls
> handle postback data.
>
> <end>
>
> I do not understand this at all. It seems to me that the Init method would
> be a safe place to initialize the child objects since that method occurs
> before loading postback data or raising postback events (in the control
life
> cycle.) What am I missing here?
>
> Much Thanks in advance.
>
> Stan
>
>
>