Thursday, December 02, 2004

Inner Content of a Custom Control

To retrieve the inner content of a custom control, you'll first have to make sure your control does not specify the ParseChildrenAttribute or specifies ParseChildrenAttribute(ChildrenAsProperties = false). Then after that, you can make use of protected void AddParsedSubObject(object obj) method to parse your inner content into a property. For example.

public class MenuItem : Control {
  private string m_href;
  private string m_text;

  public string Text {
    get { return m_text; }
    set { m_text = value; }
  }

  protected override void AddParsedSubObject(object obj) {
    if ( obj is LiteralControl ) {
      Text = ((LiteralControl)obj).Text;
    }
  }
}


But if you want to parse html, I suggest creating your own Templated Control.

Back to coding.

No comments: