UserControl中的ASP.NET内容占位符

时间:2011-03-29 18:37:52

标签: asp.net user-controls

我希望能够做到这样的事情:

<uc:MyUC>
    <CustomContent>
        <span id="name">johnny the assasin</span>
    </CustomContent>
</uc:MyUC>

并使该控件能够呈现

hello <b><span id="name">johnny the assasin</span></b>

但我似乎无法找到一种方法来公开一个允许我写任何标记的属性,就好像我的用户控件上有一个内容占位符一样

这样的事情是否可能?

2 个答案:

答案 0 :(得分:5)

我认为您需要在控件中定义类型为ITemplate的属性。 E.g。

[PersistenceMode(PersistenceMode.InnerProperty)]
public ITemplate CustomContent { get; set; }

然后你可以在标记中使用它,如:

<uc:MyUC>
    <CustomContent>
        <span id="name">johnny the assasin</span>
    </CustomContent>
</uc:MyUC> 

更多信息可以在这里找到:How to: Create Templated ASP.NET User Controls

答案 1 :(得分:1)

如果在asp.net usercontrol上设置[ParseChildren(false)]属性,则不会将html标记视为控件属性。

所以在你的班级定义中,

[ParseChildren(false)]
public class MyUC : System.Web.UI.UserControl
{

}

当然,如果你想让你的用户控件实际拥有属性,一个“按原样”呈现的属性,你可能必须创建你的主用户控件,然后让CustomContent属性是另一个只呈现内容的usercontrol。

请参阅this link for a helpful article