Setting a property based on a custom control's inner content

by timvasil 12/3/2007 11:38:00 AM

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

 

Tags:

ASP.NET

Add comment




  Country flag
biuquote
  • Comment
  • Preview
Loading


Search

Calendar

«  May 2012  »
SuMoTuWeThFrSa
293012345
6789101112
13141516171819
20212223242526
272829303112
3456789

View posts in large calendar

Recent comments

Archive