Composite controls not visible at design time

Discussion in 'ASP .Net Building Controls' started by Peter Morris [Droopy Eyes Software], Sep 8, 2004.

  1. Hi all

    I have a few questions regarding composite controls

    1) My composite controls are created based on a property of my control.
    When the postback of a page occurs due to a button click, my
    CreateChildControls() method is called before the button code is executed,
    so if that button code changes the property in question it is already too
    late to reflect this in the control. Is there a way to "Invalidate" my
    control when this property is set so that it's child controls are recreated?

    2) Dropping my component on to a WebForm reveals two behaviours which I do
    not want.
    2a) Firstly the control has a specific width/height, whereas I want it
    to size automatically based on which of the child controls are visible.
    2b) At design time, none of the child controls are visible at all

    3) My final question. One property of my control is a DateTime. If I allow
    data binding to this property how do I handle a null value from the data
    source? I have an IsNull property, but surely the data binding will try to
    bind the DateTime to null?

    Thank you
    Peter Morris [Droopy Eyes Software], Sep 8, 2004
    1. Advertisements

  2. recreated?

    Yes. Set ChildControlsCreated to false.
    That doesn't always happen to me. What is your control derived from? Does it
    set its own size?
    Composite controls should have a designer. In the GetDesignTimeHtml method,
    they should cause EnsureChildControls to be called. The basic pattern is as
    follows (note that I haven't tried to build this):

    namespace MyControls
    public class CompositeControl : WebControl, INamingContainer
    protected override ControlCollection Controls
    return base.Controls;

    public string PropertyThatDeterminesChildren
    get {return (string) ViewState["propertyThatDeterminesChildren"];}
    ViewState["propertyThatDeterminesChildren"] = value;
    ChildControlsCreated = false;

    private Label _childLabel;

    public string PropertyWhichRequiresChildren
    return _childLabel.Text;
    _childLabel.Text = value;

    protected override void CreateChildControls()
    // Create your control hierarchy here
    _childLabel = new Label();
    _childLabel.ID = "childLabel";
    base.Controls.Add(_childLabel); // Note: don't recurse

    if (PropertyThatDeterminesChildren == "button")
    Button btn = new Button();
    btn.ID = "btn";
    btn.Text = "Button";
    } else {
    LinkButton lnk = new LinkButton();
    lnk.ID = "lnk";
    lnk.Text = "LinkButton";

    namespace MyControls.Design
    public class CompositeControlDesigner : ControlDesigner
    protected override string GetDesignTimeHtml()
    // Ensure that the controls are created
    ControlCollection controls = ((CompositeControl)

    // ... then
    return base.GetDesignTimeHtml();

    Your own controls would then derive from CompositeControl.
    When you say "null", do you mean a NULL from a database? If so, then an
    exception will be thrown when an attempt is made to bind the NULL to your
    DateTime property. The same will happen no matter what kind of property you
    bind to, since whatever type it is (except for object), it isn't DBNull,
    which is a distinct type. This is no different from any other control, so
    your callers should probably do with this DateTime property whatever they
    would do with a string property.
    John Saunders, Sep 9, 2004
    1. Advertisements

  3. Hi John

    Thanks for the excellent level of detail.

    System.Web.UI.WebControls.WebControl, INamingContainer

    It doesn't set its own size in the constructor or anything.

    Seeing as I will need to do this again in the future, and will most likely
    forget the details, do you mind if I post this information on my website
    (where I keep all information I don't want to forget)? I'd be perfectly
    happy to credit the article in your name, and provide a URL link of your

    Thanks again!

    Audio compression components, DIB graphics controls, FastStrings

    Read or write articles on just about anything
    Peter Morris [Droopy Eyes Software], Sep 9, 2004
  4. You're welcome, and I hope it actually helped!
    Strange. The control I'm most recently working on is a composite control
    which renders a Button. It's quite happy to take up only the size of the
    (textless) button.
    Go ahead and post it wherever you like. I figure once it gets posted on a
    newsgroup, the world owns it.

    I'd suggest you include either a URL to this newsgroup discussion, or at
    least a textual reference to it. That way, readers of your web site will be
    able to find the discussion.
    John Saunders, Sep 9, 2004
  5. You're welcome, and I hope it actually helped!

    Yes, it did. It solved all of the problems I was experiencing except the
    size issue.
    I remember seeing a property in the designer about disallowing resize or
    something, I will try that later and see if it solves the problem.

    Thanks. I'll write it up later.

    I appreciate your help!
    Peter Morris [Droopy Eyes Software], Sep 9, 2004
  6. Peter Morris [Droopy Eyes Software], Sep 10, 2004
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.