Child controls being loaded BEFORE parent controls

Posted on 2007-08-07
Last Modified: 2013-11-07
I'm tearing my hair out here!

I have a composite server control 'FilterList' which has a collection of child controls 'FilterField' which are themselves composite controls. they're declared like this:

<cc1:FilteredList ....>
        <cc1:FilterField ...
        <cc1:FilterField ...

Now, what's confusing me is that the CreateChildControls method of the FilterField controls is being called before the CreateChildControl of the FilteredList. And also, the this.Page is always null at this point. Why would that happen? It's meaning that none of my filterField controls are holding their data via the ViewState.

Please help before I'm completely bald.
Question by:senior_internet
    LVL 1

    Expert Comment

    The OnInit method is the first method that is called in the page lifecycle.  LoadViewState is the third method so your viewstate data is not available when the child controls are initialized.  The control is stable in the Load event where it has been initialized and the state has been reconstructed.
    LVL 3

    Author Comment

    Hi, thanks for the response,

    Yeh I know about the page lifecycle but my problem is that the child controls are being created before their parent control. I want them to be created by the parent control in the CreateChildControls() method but they're not being. It seems the page is creating them so they end up with no parent (this.Parent is null) and no page (this.Page is null) and the viewstate doesn't work at all.
    LVL 1

    Accepted Solution

    Without some of your code it's difficult for me to see what is going on.  It could be as simple as forgetting to override the CreateChildControls method.

    The child controls have to be added to their parent control before any child control properties are set and where this is done in the page lifecycle makes a huge difference.

    This may help:
    LVL 3

    Author Comment

    Thanks for your help. Having found the problem now, I can appreciate that you would have needed much more code to figure it out.

    The problem was that the child controls had a property on them which called 'EnsureChildControls'. I was referencing this property too early in the page life cycle and therefore causing the CreateChildControls method to execute before it should have.


    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Join & Write a Comment

    A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
    More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…
    Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now