By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,763 Members | 1,609 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,763 IT Pros & Developers. It's quick & easy.

PostBack events fire in a different order

P: n/a
V
I have found that when I have a composite control that uses the
CreateChildControls method, on a regular page load, Page_Load executes before
CreateChildControls, 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?
Jul 21 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
JD
You may have to show a simple example that demonstrates the problem. I just
created a simple web control, threw it on a page, and Page_Load happens
before CreateChildControls everytime.

JD

"V" <V@discussions.microsoft.com> wrote in message
news:B1**********************************@microsof t.com...
I have found that when I have a composite control that uses the
CreateChildControls method, on a regular page load, Page_Load executes before CreateChildControls, 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?

Jul 21 '05 #2

P: n/a
JD
One 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

"V" <V@discussions.microsoft.com> wrote in message
news:B1**********************************@microsof t.com...
I have found that when I have a composite control that uses the
CreateChildControls method, on a regular page load, Page_Load executes before CreateChildControls, 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?

Jul 21 '05 #3

P: n/a
V
Hi 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.

"JD" wrote:
One 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

"V" <V@discussions.microsoft.com> wrote in message
news:B1**********************************@microsof t.com...
I have found that when I have a composite control that uses the
CreateChildControls method, on a regular page load, Page_Load executes

before
CreateChildControls, 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?


Jul 21 '05 #4

P: n/a
JD
> 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.
There are different events and methods called when a postback happens versus
a non-postback. LoadViewState for example happens only on a postback. What
may be happening is ASP.NET is restoring your control's state which may call
ensurechildcontrols. But I can't say unless I see a simplified example of
where this is happening.

But anyway my suggestion still stand, since you don't know when CCC is going
to be called, you shouldn't be relying CCC to happen in a specific part of
the page lifecycle. Your control should be hooking into the page events
itself.

JD

"V" <V@discussions.microsoft.com> wrote in message
news:5C**********************************@microsof t.com... Hi 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.

"JD" wrote:
One 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

"V" <V@discussions.microsoft.com> wrote in message
news:B1**********************************@microsof t.com...
I have found that when I have a composite control that uses the
CreateChildControls method, on a regular page load, Page_Load executes

before
CreateChildControls, 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?


Jul 21 '05 #5

P: n/a
JD
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


"V" <V@discussions.microsoft.com> wrote in message
news:5C**********************************@microsof t.com...
Hi 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.

"JD" wrote:
One 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

"V" <V@discussions.microsoft.com> wrote in message
news:B1**********************************@microsof t.com...
I have found that when I have a composite control that uses the
CreateChildControls method, on a regular page load, Page_Load executes

before
CreateChildControls, 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?


Jul 21 '05 #6

P: n/a
JD
Yep this is it, at least in my example.

I created a composite control that contained no controls.
CreateChildControls happened always after page load whether post back or
not. I then added a listbox to my composite control, but did not select any
choices, and again CreateChildControls happened always after page load
whether post back or not (note my composite control is the only thing on the
page). But as soon as I selected a choice from the listbox, on postback
only, the CreateChildControls is called before page load in the load post
back data phase of the page lifecycle.

So there you have the why.

JD


"JD" <no@spam.com> wrote in message news:AWSkd.22723$V41.8434@attbi_s52...
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


"V" <V@discussions.microsoft.com> wrote in message
news:5C**********************************@microsof t.com...
Hi 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.

"JD" wrote:
One 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

"V" <V@discussions.microsoft.com> wrote in message
news:B1**********************************@microsof t.com...
> I have found that when I have a composite control that uses the
> CreateChildControls method, on a regular page load, Page_Load executes before
> CreateChildControls, 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?


Jul 21 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.