为什么要在CreateChildControls中清除控件
ASP.NET 复合控件的所有示例始终将清除子控件作为 CreateChildControls 中的第一个操作。这是为什么呢?据我了解,这个方法总是通过 EnsureChildControls 调用,它设置一个标志来指示它已经被调用,因此不会再次调用 CreateChildControls。
CreateChildControls 是否被基础设施的其他方面调用?如果是在什么情况下?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
没有什么可以阻止您在控件的 OnInit 期间创建控件并将它们添加到控件集合中。然后,当调用 CreateChildControls 时,已经存在控件。这可能听起来很奇怪,但是如果您要扩展一个控件,您知道当您的控件调用基本方法(例如基本构造函数或 base.OnLoad)时该基本控件在做什么吗?
基本上,首先调用 Controls.Clear() 是一个很好的做法。
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.