Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Building Controls > What is the life cycle when there are child controls?

Reply
Thread Tools

What is the life cycle when there are child controls?

 
 
Sally
Guest
Posts: n/a
 
      04-14-2004
The basic life cycle is (hopefully I am correct):
OnInit
LoadViewState
LoadPostBackData* (loads controls with postback data)
OnLoad (form load)
RaisePostDataChangedEvent* (controls raise data change event
here)
RaisePostBackEvent* (controls raise postback events here)
OnPreRender
SaveViewState
Render
OnUnload

*only on postbacks

But what happens when there are children?
For OnInit, you cannot assume that any of order that the
children/parents are created (I am guessing that means that their
constructors haven't even been called yet). There are probably other
conditions. Anyone want to help and put the following in order?

base()
child()
base.CreateChildControls()
chld.CreateChildControls()

base.OnInit
chld.OnInit
base.LoadViewState
chld.LoadViewState
base.LoadPostBackData* (loads controls with postback data)
chld.LoadPostBackData* (loads controls with postback data)
base.OnLoad (form load)
chld.OnLoad (form load)
base.RaisePostDataChangedEvent* (controls raise data change
event here)
chld.RaisePostDataChangedEvent* (controls raise data change
event here)
base.RaisePostBackEvent* (controls raise postback events here)
chld.RaisePostBackEvent* (controls raise postback events here)
base.OnPreRender
chld.OnPreRender
base.SaveViewState
chld.SaveViewState
base.Render
chld.Render
base.OnUnload
chld.OnUnload
 
Reply With Quote
 
 
 
 
Scott Mitchell [MVP]
Guest
Posts: n/a
 
      04-15-2004
> The basic life cycle is (hopefully I am correct):
> OnInit
> LoadViewState
> LoadPostBackData* (loads controls with postback data)
> OnLoad (form load)
> RaisePostDataChangedEvent* (controls raise data change event
> here)
> RaisePostBackEvent* (controls raise postback events here)
> OnPreRender
> SaveViewState
> Render
> OnUnload
>
> *only on postbacks
>
> But what happens when there are children?


Sally, the control hierarchy is enumerated in a depth-first search
order. That is, the order the events transpire in the control hierarchy
can be shown with this pseudocode:

FUNCTION SomeFunction(parent)
FOR EACH child IN parent
SomeFunction(child)
END FUNCTION

So, if you had a control hiearchy like:

C1
/ \
C2 C7
/ | \ \
C3 C5 C6 C8
|
C4

The order the controls would be visited would be C1, C2, ..., C8. To
get to the point, you DO know the order the Init event will fire in the
controls. Namely, a parent control will always be initialized (or
loaded, or prerendered, etc.) BEFORE its children.

Hope this helps / makes sense!

--

Scott Mitchell
http://www.velocityreviews.com/forums/(E-Mail Removed)
http://www.4GuysFromRolla.com
http://www.ASPFAQs.com
http://www.ASPMessageboard.com

* When you think ASP, think 4GuysFromRolla.com!
 
Reply With Quote
 
 
 
 
Sally
Guest
Posts: n/a
 
      04-16-2004
Thanks Scott, but account to the .net Doc for control.oninit in the
remarks section, I read:

Remarks
When notified by this method, server controls must perform any
initialization steps that are required to create and set up an
instance. In this stage of the server control's lifecycle, the
control's view state has yet to be populated. Additionally, you can
not access other server controls when this method is called either,
regardless of whether it is a child or parent to this control. Other
server controls are not certain to be created and ready for access

Someone posted somewhere that OnInit might be delayed as long as
prerender.

If this is true, then OnLoad can be called on an object before oninit
is called for a child. This is why I am confused about.

-Sal

"Scott Mitchell [MVP]" <(E-Mail Removed)> wrote in message news:<5byfc.37064$(E-Mail Removed) .com>...
> > The basic life cycle is (hopefully I am correct):
> > OnInit
> > LoadViewState
> > LoadPostBackData* (loads controls with postback data)
> > OnLoad (form load)
> > RaisePostDataChangedEvent* (controls raise data change event
> > here)
> > RaisePostBackEvent* (controls raise postback events here)
> > OnPreRender
> > SaveViewState
> > Render
> > OnUnload
> >
> > *only on postbacks
> >
> > But what happens when there are children?

>
> Sally, the control hierarchy is enumerated in a depth-first search
> order. That is, the order the events transpire in the control hierarchy
> can be shown with this pseudocode:
>
> FUNCTION SomeFunction(parent)
> FOR EACH child IN parent
> SomeFunction(child)
> END FUNCTION
>
> So, if you had a control hiearchy like:
>
> C1
> / \
> C2 C7
> / | \ \
> C3 C5 C6 C8
> |
> C4
>
> The order the controls would be visited would be C1, C2, ..., C8. To
> get to the point, you DO know the order the Init event will fire in the
> controls. Namely, a parent control will always be initialized (or
> loaded, or prerendered, etc.) BEFORE its children.
>
> Hope this helps / makes sense!
>
> --
>
> Scott Mitchell
> (E-Mail Removed)
> http://www.4GuysFromRolla.com
> http://www.ASPFAQs.com
> http://www.ASPMessageboard.com
>
> * When you think ASP, think 4GuysFromRolla.com!

 
Reply With Quote
 
Victor Garcia Aprea [MVP]
Guest
Posts: n/a
 
      04-25-2004
Hi Scott,

>>>Namely, a parent control will always be initialized (or
>>> loaded, or prerendered, etc.) BEFORE its children.


This is actually not completely right. Init is a special case that will fire
BEFORE in childrens than parents.

--
Victor Garcia Aprea
Microsoft MVP | ASP.NET
Looking for insights on ASP.NET? Read my blog:
http://obies.com/vga/blog.aspx
To contact me remove 'NOSPAM'. Please post all questions to the newsgroup

"Scott Mitchell [MVP]" <(E-Mail Removed)> wrote in message
news:5byfc.37064$(E-Mail Removed). com...
> > The basic life cycle is (hopefully I am correct):
> > OnInit
> > LoadViewState
> > LoadPostBackData* (loads controls with postback data)
> > OnLoad (form load)
> > RaisePostDataChangedEvent* (controls raise data change event
> > here)
> > RaisePostBackEvent* (controls raise postback events here)
> > OnPreRender
> > SaveViewState
> > Render
> > OnUnload
> >
> > *only on postbacks
> >
> > But what happens when there are children?

>
> Sally, the control hierarchy is enumerated in a depth-first search
> order. That is, the order the events transpire in the control hierarchy
> can be shown with this pseudocode:
>
> FUNCTION SomeFunction(parent)
> FOR EACH child IN parent
> SomeFunction(child)
> END FUNCTION
>
> So, if you had a control hiearchy like:
>
> C1
> / \
> C2 C7
> / | \ \
> C3 C5 C6 C8
> |
> C4
>
> The order the controls would be visited would be C1, C2, ..., C8. To
> get to the point, you DO know the order the Init event will fire in the
> controls. Namely, a parent control will always be initialized (or
> loaded, or prerendered, etc.) BEFORE its children.
>
> Hope this helps / makes sense!
>
> --
>
> Scott Mitchell
> (E-Mail Removed)
> http://www.4GuysFromRolla.com
> http://www.ASPFAQs.com
> http://www.ASPMessageboard.com
>
> * When you think ASP, think 4GuysFromRolla.com!



 
Reply With Quote
 
Victor Garcia Aprea [MVP]
Guest
Posts: n/a
 
      04-25-2004
Hi Sally,

In your list, LoadViewState is missing an '*' as it only will execute on
postbacks. Note that there is no sense in executing LoadViewState on
non-postbacks as there won't be any state to load in the first place.

Also in your list, there is a missing step which is commonly known as
"LoadPostData second try" which happens right after Load and just before
RaiseChangedEvents. This one is for giving a chance to controls that are
created at Load to catchup with posted data.

Regarding CreateChildControls it won't fire necessary where you're listing
it. The point at where it will fire will depend if you're dealing with a
postback or not and on how soon the controls need to be accessed.

Let me know if this helps,

--
Victor Garcia Aprea
Microsoft MVP | ASP.NET
Looking for insights on ASP.NET? Read my blog:
http://obies.com/vga/blog.aspx

To contact me remove 'NOSPAM'. Please post all questions to the newsgroup
"Sally" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> The basic life cycle is (hopefully I am correct):
> OnInit
> LoadViewState
> LoadPostBackData* (loads controls with postback data)
> OnLoad (form load)
> RaisePostDataChangedEvent* (controls raise data change event
> here)
> RaisePostBackEvent* (controls raise postback events here)
> OnPreRender
> SaveViewState
> Render
> OnUnload
>
> *only on postbacks
>
> But what happens when there are children?
> For OnInit, you cannot assume that any of order that the
> children/parents are created (I am guessing that means that their
> constructors haven't even been called yet). There are probably other
> conditions. Anyone want to help and put the following in order?
>
> base()
> child()
> base.CreateChildControls()
> chld.CreateChildControls()
>
> base.OnInit
> chld.OnInit
> base.LoadViewState
> chld.LoadViewState
> base.LoadPostBackData* (loads controls with postback data)
> chld.LoadPostBackData* (loads controls with postback data)
> base.OnLoad (form load)
> chld.OnLoad (form load)
> base.RaisePostDataChangedEvent* (controls raise data change
> event here)
> chld.RaisePostDataChangedEvent* (controls raise data change
> event here)
> base.RaisePostBackEvent* (controls raise postback events here)
> chld.RaisePostBackEvent* (controls raise postback events here)
> base.OnPreRender
> chld.OnPreRender
> base.SaveViewState
> chld.SaveViewState
> base.Render
> chld.Render
> base.OnUnload
> chld.OnUnload



 
Reply With Quote
 
Victor Garcia Aprea [MVP]
Guest
Posts: n/a
 
      04-25-2004
Hi Sally,

I've already replied to your first post, take a look at that and let me know
if that helped.

Now, regarding your new questions:

The docs are correct -- yesss, believe it or not they're correct . At
Init viewstate won't be loaded yet and you shouldn't assume anything in
there. Also, keep in mind that Init for childs will fire before Init for
their parents (you can't count on that one).

>>> Someone posted somewhere that OnInit might be delayed as long as
>>> prerender.

Not sure if you're referring to how/when CreateChildControls run. In some
cases CreateChildControls won't run until the parent control's PreRender is
fired. Let me know what specific scenario you're thinking about.

>>> If this is true, then OnLoad can be called on an object before oninit
>>> is called for a child. This is why I am confused about.

If the child is specified at design-time then NO, the Init for the child
will always run before the Load of its parent.
If the child is dynamically created for example at parent's OnLoad, then
YES, Init and Load for the parent will run before Init of the child. This is
commonly known as playing "catch up", which consists of a child control
trying to catchup up to the same state that its parent once it is added to
its parent's Control collection. You can learn more about this particular
topic here[1]

Let me know if you still have any doubts about this,

[1] http://weblogs.asp.net/vga/archive/2.../11/23498.aspx

--
Victor Garcia Aprea
Microsoft MVP | ASP.NET
Looking for insights on ASP.NET? Read my blog:
http://obies.com/vga/blog.aspx
To contact me remove 'NOSPAM'. Please post all questions to the newsgroup

"Sally" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> Thanks Scott, but account to the .net Doc for control.oninit in the
> remarks section, I read:
>
> Remarks
> When notified by this method, server controls must perform any
> initialization steps that are required to create and set up an
> instance. In this stage of the server control's lifecycle, the
> control's view state has yet to be populated. Additionally, you can
> not access other server controls when this method is called either,
> regardless of whether it is a child or parent to this control. Other
> server controls are not certain to be created and ready for access
>
> Someone posted somewhere that OnInit might be delayed as long as
> prerender.
>
> If this is true, then OnLoad can be called on an object before oninit
> is called for a child. This is why I am confused about.
>
> -Sal
>
> "Scott Mitchell [MVP]" <(E-Mail Removed)> wrote in message

news:<5byfc.37064$(E-Mail Removed) .com>...
> > > The basic life cycle is (hopefully I am correct):
> > > OnInit
> > > LoadViewState
> > > LoadPostBackData* (loads controls with postback data)
> > > OnLoad (form load)
> > > RaisePostDataChangedEvent* (controls raise data change event
> > > here)
> > > RaisePostBackEvent* (controls raise postback events here)
> > > OnPreRender
> > > SaveViewState
> > > Render
> > > OnUnload
> > >
> > > *only on postbacks
> > >
> > > But what happens when there are children?

> >
> > Sally, the control hierarchy is enumerated in a depth-first search
> > order. That is, the order the events transpire in the control hierarchy
> > can be shown with this pseudocode:
> >
> > FUNCTION SomeFunction(parent)
> > FOR EACH child IN parent
> > SomeFunction(child)
> > END FUNCTION
> >
> > So, if you had a control hiearchy like:
> >
> > C1
> > / \
> > C2 C7
> > / | \ \
> > C3 C5 C6 C8
> > |
> > C4
> >
> > The order the controls would be visited would be C1, C2, ..., C8. To
> > get to the point, you DO know the order the Init event will fire in the
> > controls. Namely, a parent control will always be initialized (or
> > loaded, or prerendered, etc.) BEFORE its children.
> >
> > Hope this helps / makes sense!
> >
> > --
> >
> > Scott Mitchell
> > (E-Mail Removed)
> > http://www.4GuysFromRolla.com
> > http://www.ASPFAQs.com
> > http://www.ASPMessageboard.com
> >
> > * When you think ASP, think 4GuysFromRolla.com!



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How do I: Main thread spawn child threads, which child processes...control those child processes? Jeff Rodriguez C Programming 23 12-09-2003 11:06 PM
Re: ASP Life Cycle Problem S. Justin Gengo ASP .Net 0 12-08-2003 02:22 PM
ASP Life Cycle Problem john doe Jr ASP .Net 1 12-05-2003 07:03 PM
Life cycle order for parent child relations foldface@yahoo.co.uk ASP .Net Web Controls 7 11-22-2003 12:15 PM



Advertisments