I want to define a custom ASP.NET control whose default property, say Text, gets set to the inner content the tag:
<prefix:MyTag runat="server" id="myTag">
Inner content
</prefix:MyTag>
makes myTag.Text == "Inner content" true.
There are several approaches:
-
Override AddParsedSubObject and extract the text from added LiteralControls, throwing exceptions for other conditions, or
-
Override Render to use the Text getter, and implement the Text getter to render the contained controls into a StringWriter wrapped in an HtmlTextWriter.
-
Use special attributes:
[ParseChildren(true, "Text")]
[PersistChildren(false)]
public class MyTag : Control
{
private string _text
public ScriptVariableDefinition()
{
}
[PersistenceMode(PersistenceMode.InnerDefaultProperty)]
public string Text
{
get { return _text}
set { _text= value; }
}
}