开发者

ASP.NET content placeholder in UserControl

I want to be able to do something like this:

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

and having that control able to render

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

but I can't seem to find a way to expose a property that lets me write any markup I want j开发者_高级运维ust like as if I had a content place holder on my user control

Is something like this even possible?


I think you need to define a property of type ITemplate in your control. E.g.

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

and then you could use it in markup like:

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

More could be found here: How to: Create Templated ASP.NET User Controls


If you set the [ParseChildren(false)] attribute on an asp.net usercontrol, then it will not treat the html tags as control properties.

So in your class definition,

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

}

Of course, if you want to have your user control actually have properties, and a property that is rendered 'as is', you may have to create your main user control, and then have the CustomContent property be another usercontrol that just renders the content.

See this link for a helpful article.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜