EnsureChildControls?

Rate It (2)

Last post 06-26-2009 3:54 PM by Brijesh50. 3 replies.

Sort Posts:

  • EnsureChildControls?

    04-24-2007, 11:02 AM
    • Contributor
      2,317 point Contributor
    • nisarkhan
    • Member since 10-31-2005, 5:55 PM
    • Planet Earth
    • Posts 1,361

    Could somebody give me a better feel for the why's and when's of EnsureChildControls?

    I understand what it does ( calls CreateChildControls if needed ) , but I don't get when or why I would call it.


    what's the senario?

    Thanks

    Its all about coding!
    --------------------------
    Don't forget to click "Mark as Answer" on the post(s) that helped you.
  • Re: EnsureChildControls?

    04-24-2007, 5:36 PM
    Answer
    • Member
      338 point Member
    • scobrown
    • Member since 04-24-2007, 8:52 PM
    • Atlanta, Ga
    • Posts 49
    Simple example: 

    Lets say you have class FooObject with a string property Bar, and the following property Foo in your Control: 

     

    private FooObject _foo;
    public string Foo
    {
        get
        {
            return _foo.Bar;
        }
        set
        {
            _foo.Bar = value;
        }
    }

     

    If you initialize _foo in CreateChildControls, then this code could throw a null reference error.  The problem would occur if you try to set the value of Foo in the Designer:

    <YourControl runat="server" id="newControl" Foo="This will cause a null reference" /> 

    Properties set in the Designer will be set at runtime before any calls to CreateChildControls.  You could get lucky if some other property gets set before yours and it calls CreateChildControls.  Imagine the .net runtime making the following series of call based on the above:

    YourControl newControl = new YourControl();
    newControl.Foo = "This will cause a null reference";
    ParentControl.Controls.Add(newControl);

     Anytime you are initializing an object in CreateChildControls, you should wrap it's property accessor with EnsureChildControls.  Even if you do the following:

    private FooObject _foo = new FooObject();
    public string Foo
    {
        get
        {
            return _foo.Bar;
        }
        set
        {
            _foo.Bar = value;
        }

    If you set _foo to a new value in CreateChildControls(), the new value will have the default Bar value and not the new one.  When creating a control, most properties you expose will operate on private variables whose value will be set in CreateChildControls(), so you should always add the EnsureChildControls.

    Scott Brown
  • Re: EnsureChildControls?

    04-24-2007, 5:39 PM
    Answer
    • Member
      338 point Member
    • scobrown
    • Member since 04-24-2007, 8:52 PM
    • Atlanta, Ga
    • Posts 49
    I guess the short and sweet answer is that CreateChildControls() is not called by the constructor.
    Scott Brown
  • Re: EnsureChildControls?

    06-26-2009, 3:54 PM
    • Member
      2 point Member
    • Brijesh50
    • Member since 06-26-2009, 3:44 PM
    • Posts 1

    While reading an article on MSN for creating Custom Templated Control, the override method for DataBind calls CreateChildControls as follows

    protected override CreateChildControls()

    {

    //Logic for creating TemplateContainer

    //Logic for calling ITemplate.InstantiateIn()

    //Logic for adding TemplateContainer to Controls Collection.

    }


    protected override DataBind()

    {

     CreateChildControls();

    ChildControlsCreated  = true;

    base.DataBind();

    }


    Another book I was reading did have the same logic for CreateChildControls but on databind does the following

    protected override DataBind()

    {

       EnusreChildControls();

       base.DataBind();

    }


    I am confused as to whats the recommended practise?

    Please Advise

    Thanks

    Brijesh

Page 1 of 1 (4 items)