开发者

Why clear controls in CreateChildControls

All of th开发者_Python百科e 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?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜