Frage

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?

War es hilfreich?

Lösung

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:

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top