Child controls being loaded BEFORE parent controls

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.
Who is Participating?
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:
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.
senior_internetAuthor Commented:
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.
senior_internetAuthor Commented:
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.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.