简体   繁体   中英

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.

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. Generally when I need templates, I simply use PlaceHolder instead.

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

    ... render stuff

}

Here 's an example on MSDN:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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