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

431 views Asked by At

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?

1

There are 1 answers

2
Chris Gessler On BEST ANSWER

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: