我是ASP .NET Web控件的新手,但不是ASP.NET或C#的新手。

我想知道如何将允许的内容类型限制为特定的类。

我做了一个名为TabPanel的自定义Web控件,我希望它只能包含TabPages。

例如,以下标记应该是非法的,因为它包含一个复选框。

<cc1:TabPanel ID="TabPanel1" runat="server">
    <cc1:TabPage runat="server">
        this is a simple test
    </cc1:TabPage>
    <cc1:TabPage runat="server">
        this is another simple test
    </cc1:TabPage>
    <asp:CheckBox runat="server" />
</cc1:TabPanel>


在这种情况下,我不希望该复选框存在。如何阻止这种情况发生?

最佳答案

我没有完全尝试过您想要的,但是根据我做过的其他事情,我将尝试以下操作:


在TabPannel中创建一个属性,该属性是TabPage的集合(出于演示目的,将其称为Tabs)。它可以是数组,列表或自定义集合类,关键是要键入以仅接受TabPage作为成员。
为属性分配[PersistenceMode(PersistenceMode.InnerProperty)]属性。
重写CreateChildControls可以将集合的内容添加到控件中。


如果您采用这种方式,那么您的标记最终应该看起来像这样:

<cc1:TabPanel ID="TabPanel1" runat="server">
    <Tabs>
        <cc1:TabPage runat="server">this is a simple test</cc1:TabPage>
        <cc1:TabPage runat="server">this is another simple test</cc1:TabPage>
    </Tabs>
</cc1:TabPanel>


并且它不应允许将非TabPage的任何内容嵌套在Tabs属性内。

http://msdn.microsoft.com/en-us/library/9txe1d4x(v=VS.90).aspx是详细演示此技术的演练。

关于c# - ASP.NET Web控件仅允许特定内容?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/5680556/

10-10 01:06
查看更多