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

 

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

ASP.NET

Related posts

Comments are closed

 

About the author

Tim Vasil Tim Vasil
I'm a software engineer living in Cambridge, MA.

E-mail me Send mail

Search

Calendar

<<  September 2010  >>
MoTuWeThFrSaSu
303112345
6789101112
13141516171819
20212223242526
27282930123
45678910

View posts in large calendar

Recent comments