如何阅读asp.net服务器控件的标签之间的内容

时间:2012-05-17 11:48:54

标签: asp.net custom-server-controls servercontrols

我正在编写一个asp.net服务器端控件,它有一些简短的参数传递给它,但也需要允许提供大量的自定义HTML代码。

最简单的方法我想是允许在服务器控件的标签之间指定,如下所示:

<MyControl:Example Runat="server" Id="myControl" Message="This is a message">
  <p>This is a long piece of HTML a few dozen lines long...</p>
</MyControl>

如何从自定义服务器控件中访问标记之间的文本?

1 个答案:

答案 0 :(得分:3)

您需要创建模板化控件:

<MyControl:Example Runat="server" Id="myControl" Message="This is a message"> 
  <HtmlContent><p>This is a long piece of HTML a few dozen lines long...</p></HtmlContent>
</MyControl> 

HtmlContent是您的模板。通常,当我需要模板时,我只需使用PlaceHolder。

public class MyControl : CompositeControl 
{ 
    [TemplateContainer(typeof(PlaceHolder))] 
    [PersistenceMode(PersistenceMode.InnerProperty)] 
    public PlaceHolder HtmlContent { get; set; } 

    ... render stuff

}

Here是MSDN的一个例子: