为什么会出现“MetaTag 不允许使用子控件”这样的异常?
在《ASP.Net 服务器控件与组件开发》的P103页有一个例子,简略代码如下:
[DefaultProperty("Name")]
public class MetaTag : Control
{
...
// This ensures that there are no child controls
protected override ControlCollection CreateControlCollection()
{
return new EmptyControlCollection(this);
}
...
}
书上说重载 CreateControlCollection() 函数并返回一个EmptyControlCollection实例是为了“通过不接受用户在控件中加入子控件可以保证Controls集合为空。
可是当我使用此控件时却引发了“MetaTag 不允许使用子控件”这样的异常,而将CreateControlCollection()函数删掉就没问题。
请问这是为什么?