除了创建自定义ControlBuilder
类之外,是否有办法阻止在自定义ASP.NET控件中将服务器控件添加为子项?
例如,假设我正在构建自己的Panel
控件:
<my:SpecialPanel ID="SpecialPanel1" runat="server">
<!-- Allow valid HTML -->
<input id="tbEmailAddress" type="text" />
</my:SpecialPanel>
我想阻止用户在SpecialPanel
:
<my:SpecialPanel ID="SpecialPanel1" runat="server">
<!-- WRONG - Throw an Exception -->
<asp:TextBox ID="tbEmailAddress" runat="server" />
</my:SpecialPanel>
有什么建议吗?
答案 0 :(得分:1)
您可以覆盖Control.AddParsedSubObject方法:
public class SpecialPanel : Control
{
protected override void AddParsedSubObject(Object obj)
{
if (obj is Control)
{
throw new InvalidOperationException(
"The 'SpecialPanel' control cannot contain server controls");
}
}
}
相关资源: