What is the life cycle when there are child controls?

S

Sally

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
 
S

Scott Mitchell [MVP]

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 address removed)
http://www.4GuysFromRolla.com
http://www.ASPFAQs.com
http://www.ASPMessageboard.com

* When you think ASP, think 4GuysFromRolla.com!
 
S

Sally

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
 
V

Victor Garcia Aprea [MVP]

Hi Scott,

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
 
V

Victor Garcia Aprea [MVP]

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
 
V

Victor Garcia Aprea [MVP]

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).
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 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/2003/08/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 said:
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



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 address removed)
http://www.4GuysFromRolla.com
http://www.ASPFAQs.com
http://www.ASPMessageboard.com

* When you think ASP, think 4GuysFromRolla.com!
[/QUOTE]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,015
Latest member
AmbrosePal

Latest Threads

Top