没有占位符标记的自定义ASP.NET模板控件

时间:2009-08-28 21:16:15

标签: asp.net templates custom-controls

假设我有一个自定义模板控件,可以在如下页面中使用:

<AR:CustomControl runat="server">
    <ViewTemplate>
        <span>stuff</span>
    </ViewTemplate>
</AR:ModalLink>

由于只有一个模板字段,因此该元素不是必需的。有没有办法可以编辑我的控件,所以它可以像?

一样使用
<AR:CustomControl runat="server">
    <span>stuff</span>
</AR:ModalLink>

请注意,这是一个带有ASCX文件的UserControl。所以我认为我不能简单地继承像Label这样的现有控件。

1 个答案:

答案 0 :(得分:0)

在这里找到解决方案: “http://alvinzc.blogspot.com/2006/10/aspnet-basic-of-custom-server-control_25.html

简而言之:

[ParseChildren(true, "Content")]
[PersistChildren(false)]
public partial class CustomControl : System.Web.UI.UserControl
{
    ...

    [Browsable(false),
    PersistenceMode(PersistenceMode.InnerDefaultProperty),
    TemplateContainer(typeof(Contents)),
    TemplateInstance(TemplateInstance.Single)]
    public ITemplate Content { ... }
}