views:

121

answers:

1

All of the examples of ASP.NET composite controls always clear child controls as the first action within CreateChildControls. Why is this? As I understand it this method is always called via EnsureChildControls which sets a flag to indicate that it has already been called and thus does not call CreateChildControls again.

Is CreateChildControls called by other aspects of the infrastructure? If so under what circumstances?

A: 

There is nothing to stop you from creating controls and adding them to the control collection during the OnInit of the control. Then when CreateChildControls is called, there is already controls present. This might sound wacky, but what if you are extending a control, do you know what that base control is doing when your control calls the base methods (like the base constructor, or the base.OnLoad)?

Basically, calling Controls.Clear() first is good practice.

slugster