What is the life cycle when there are child controls?

Discussion in 'ASP .Net Building Controls' started by Sally, Apr 14, 2004.

  1. Sally

    Sally Guest

    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
    Sally, Apr 14, 2004
    #1
    1. Advertising

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

    * When you think ASP, think 4GuysFromRolla.com!
    Scott Mitchell [MVP], Apr 15, 2004
    #2
    1. Advertising

  3. Sally

    Sally Guest

    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]" <> wrote in message news:<5byfc.37064$>...
    > > 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.4GuysFromRolla.com
    > http://www.ASPFAQs.com
    > http://www.ASPMessageboard.com
    >
    > * When you think ASP, think 4GuysFromRolla.com!
    Sally, Apr 16, 2004
    #3
  4. 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]" <> wrote in message
    news:5byfc.37064$...
    > > 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.4GuysFromRolla.com
    > http://www.ASPFAQs.com
    > http://www.ASPMessageboard.com
    >
    > * When you think ASP, think 4GuysFromRolla.com!
    Victor Garcia Aprea [MVP], Apr 25, 2004
    #4
  5. 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" <> wrote in message
    news:...
    > 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
    Victor Garcia Aprea [MVP], Apr 25, 2004
    #5
  6. 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/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" <> wrote in message
    news:...
    > 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]" <> wrote in message

    news:<5byfc.37064$>...
    > > > 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.4GuysFromRolla.com
    > > http://www.ASPFAQs.com
    > > http://www.ASPMessageboard.com
    > >
    > > * When you think ASP, think 4GuysFromRolla.com!
    Victor Garcia Aprea [MVP], Apr 25, 2004
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. john doe Jr

    ASP Life Cycle Problem

    john doe Jr, Dec 5, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    813
    Teemu Keiski
    Dec 5, 2003
  2. S. Justin Gengo

    Re: ASP Life Cycle Problem

    S. Justin Gengo, Dec 8, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    458
    S. Justin Gengo
    Dec 8, 2003
  3. Fred
    Replies:
    5
    Views:
    6,244
    Karl Seguin [MVP]
    Jan 23, 2006
  4. HP
    Replies:
    3
    Views:
    597
  5. Life cycle order for parent child relations

    , Nov 16, 2003, in forum: ASP .Net Web Controls
    Replies:
    7
    Views:
    278
    Anders Borum
    Nov 22, 2003
Loading...

Share This Page