OK, doing a little more research.
EnsureChildControls is a method of the Control class. It does a virtual call
to CreateChildControls. You override CreateChildControls, which means when
EnsureChildControls calls CreateChildControls, its your method that gets
called.
I did a look to see who in the System.Web.dll calls EnsureChildControls.
There where two that look interesting:
PreRenderRecursiveInternal method of the Control class. Most likely
called at prerender that happens after the page load.
FindControl method of the Control class. Some methods who use this are
ProcessPostData and RaisePostBackEvent methods of the Page class.
The one that looks interesting here is ProcessPostData. What this method
does is go through looking for controls using FindControl, which of course
calls EnsureChildControls, and checks for controls that implement
IPostBackDataHandler interface and calls on this interface the method
LoadPostData. LoadPostData, in the page lifecycle, gets called during the
Load Postback Data phase which is before page load, and only happens on
postback. This is documented.
If I had to guess this is the guy. But I'm guessing since you did not
provide a sample or the callstack output. Tomorrow I may give it a try and
see if I can replicate it.
But again CreateChildControls purpose is to create the contained controls.
Relying on it to happen at a certain time in the page lifecycle is probably
wrong since its not documented in the page lifecycle, and hence can be
called at anytime.
JD
Post by VHi JD. None of my properties call EnsureChildControls - they don't need to.
The only thing that does call it is Databind and that is only called from
Page_Load. I searched throughout my code to make sure that
EnsureChildControls is not called from anywhere else. Also, from what you
would describe, it seems that CCC would execute before Page_Load in all
circumstances, not just postback. What I am experiencing only happens
through PostBack. We don't override any postback events either. Lastly,
when I put a breakpoint in to the CCC method, the call stack shows that
ASP.NET called CCC, not any explicit code.
Does anyone know why this would be happening on PostBacks only?
TIA
-Vanessa
P.S. I apologize for the multiple original posts - the MS web page
specifically said my first few posts faild - apparently they did not.
Post by JDOne thing I forgot and I apologize.
When creating a composite control, and it has properties or methods that
access any contained controls, the method or property should call
EnsureChildControls method before trying to access the contained control.
EnsureChildControls will call the CreateChildControls method, this will
ensure all child controls are created.
So what this means, in one page rendering if you don't access these methods
or properties, CreateChildControls will happen after page load naturally. If
in the next rendering, say you access one of the properties or methods in
the load view state method, your CreateChildControls method will be called
then, before the page load.
What I suggest is you shouldn't rely on page events within the
CreateChildControl method. Your control should probably hook into the page
events itself and process the logic there.
JD
Post by VI have found that when I have a composite control that uses the
CreateChildControls method, on a regular page load, Page_Load executes
before
Post by VCreateChildControls, but on a postback it is the reverse. This is causing
logic problems and it really bothers me that this sequence of events fires
inconsistently. Can anyone tell me why this happens and how we might get
around it?