简体   繁体   English

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

[英]How to read content between tags of asp.net server control

I'm writing an asp.net server side control which has a few short parameters passed into it, but also the need to allow a large piece of custom HTML code to be supplied. 我正在编写一个asp.net服务器端控件,它有一些简短的参数传递给它,但也需要允许提供大量的自定义HTML代码。

The easiest way of doing so I thought was to allow to be specified between the tags of the server control, like so: 最简单的方法我想是允许在服务器控件的标签之间指定,如下所示:

<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>

How can I access the text between the tags from inside my custom server control? 如何从自定义服务器控件内部访问标签之间的文本?

You need to create a templated control: 您需要创建模板化控件:

<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> 

Where HtmlContent is your template. HtmlContent是你的模板。 Generally when I need templates, I simply use PlaceHolder instead. 通常,当我需要模板时,我只需使用PlaceHolder。

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

    ... render stuff

}

Here 's an example on MSDN: 以下是MSDN上的一个示例:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM