CreateChildControls executes on initial load and at postback?

Discussion in 'ASP .Net Building Controls' started by paul reed, Feb 3, 2004.

  1. paul reed

    paul reed Guest

    Hello,

    I have a composite control I have built. I capture a click event of a tree
    control then raise my own event along with a custom args object for the
    server-side to deal with. All the plumbing seems to work just fine. However,
    I noticed, while in debug, that the CreateChildControls method gets invoked
    at initial load time, as expected, but also when I click on my tree node. It
    executes the CreateChildControls method again before executing my handler
    logic that captures the click event.

    Is there something I need to do like is done for multiple postbacks to an
    aspx page (e.g., If NOT PageIsPostback) or something like that to avoid this
    double execution of this method?

    Thanks,

    Paul
    paul reed, Feb 3, 2004
    #1
    1. Advertising

  2. Hi Paul,

    Thank you for posting in the community!

    Based on my understanding, you create composite control in Web Form, your
    control contains TreeView web control. When you click treenode of your
    treeview control, the CreateChildControls method executes before the Click
    event of your treenode control.

    ========================================================
    Based on your statement, I think you set the TreeView's AutoPostBack
    property to true, so each time you click treenode, the whole web form
    postback to server side.

    The CreateChildControls method is used to create all the child controls.
    Before you manipulate the control's properties and events, you should
    always make sure that all the child controls is created. You can achieve
    this through EnsureChildControls method. This method will invoke the
    CreateChildControls method to create all the child controls.

    In Asp.net WebForm's event model, for composition control, it will load the
    default items based in the CreateChildControls method at Init event.(Which
    is the first event)

    So CreateChildControls method is always fires before any other customized
    child control event. This is by the design of Asp.Net WebForm event model.

    You can find more information in some articles:
    http://aspalliance.com/articleViewer.aspx?aId=345&pId=-1
    http://www.codeproject.com/aspnet/composite_controls.asp

    Can you show me your real obstacles?
    Maybe I can help you create a better structure to workaround.

    ========================================================
    Thank you for your patience and cooperation. If you have any questions or
    concerns, please feel free to post it in the group. I am standing by to be
    of assistance.

    Best regards,
    Jeffrey Tan
    Microsoft Online Partner Support
    Get Secure! - www.microsoft.com/security
    This posting is provided "as is" with no warranties and confers no rights.
    Jeffrey Tan[MSFT], Feb 4, 2004
    #2
    1. Advertising

  3. paul reed

    Paul Reed Guest

    Jeffrey,

    Thanks for your help. I think I understand your points and Nic's article
    you referred me to wass great. However, I think I am doing everything
    just fine. Here are my issues:

    1. When I run under debug, I can see my control being created correctly
    in the CreateChildControls method. I then access a database, get the
    data for my treeview and load it. I also wire up an event handler to
    deal with the click event of the node because I want to raise a custom
    even back to the client. This all appears to work just fine.

    2. I do want the form to post back when a certain tree node is
    clicked...at postback time, I notice that prior to my event getting
    raised, the CreateChildControls method fires yet again, prior to the
    click event happening. In the end, my aspx page does get the custom even
    raised and I am able to assess which node was selected...but why does
    the CreateChildControls method fire on postback prior to my event
    getting handled?

    Paul Reed
    www.jacksonreed.com

    *** Sent via Developersdex http://www.developersdex.com ***
    Don't just participate in USENET...get rewarded for it!
    Paul Reed, Feb 6, 2004
    #3
  4. Hi Paul,

    Thanks very much for your feedback. I am glad I can help you :)

    For your further concern, I will explain it for your.

    ============================================
    In .Net Web Form application, because of the Http stateless, all the .Net
    objects will only existed at server side.
    So each time the Web Form renderred to the client, all the objects will be
    disposed. While the client postback to the server, all the objects will be
    created again.

    The server side event(Such as TreeNode's SelectedIndexChange event) is
    associated with the object, so its can only be handled after the objects
    have been(That is CreateChildControls method).

    Thats why CreateChildControls method fires prior to the treenode event.

    Also, I want to note you that, the Server side event is handled through
    IPostBackEventHandler interface.(You can view the IPostBackEventHandler in
    the object lifecycle model's position)

    For an example of Server side event handling, please refer to:
    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/htm
    l/cpconPostbackEventSample.asp

    ============================================
    Hope all these help you. If you still have any further concern, please feel
    free to post, I will help you.

    Best regards,
    Jeffrey Tan
    Microsoft Online Partner Support
    Get Secure! - www.microsoft.com/security
    This posting is provided "as is" with no warranties and confers no rights.
    Jeffrey Tan[MSFT], Feb 7, 2004
    #4
  5. paul reed

    Paul Reed Guest

    Jeffrey,

    One more question...thanks.

    Let's say you have a composite control with two text boxes. Let's also
    say that you initialize these conrtrols to a default value prior to have
    the control presented to the client in the browser. Let's now say the
    user changes one of those text boxes. How is it then that at post back
    time, if CreateChildControls get's fired again, you don't end up
    resetting that text box to its default value again prior to getting the
    new value that the user just keyed in?

    I am very comfortable with the postback behavior in an ASPX page, and I
    am very comfortable that at Page_Load time you can say IF Not
    (Me.IsPostBack)...to prevent doing just what I am describing, reloading
    of list boxes and default values. If it is a subsequent post back, the
    page can act accordingly and NOT do something that should only be done
    on the first execution of Page_Load. I don't know if I am making sense
    or not. I just expected the same thing with the composite control.

    Thanks again.

    Paul Reed
    www.jacksonreed.com

    *** Sent via Developersdex http://www.developersdex.com ***
    Don't just participate in USENET...get rewarded for it!
    Paul Reed, Feb 7, 2004
    #5
  6. Hi Paul,

    Thanks for your feedback.

    For your post, I am not sure of the meanning of "How is it then that at
    post back time, if CreateChildControls get's fired again, you don't end up
    resetting that text box to its default value again prior to getting the new
    value that the user just keyed in?"

    Based on my understanding, your question is: In composite control's
    postback time, CreateChildControls fires, so it initializes all the child
    controls to their default value. How does the composite control get its
    child controls' NEW value. (That is: the value that entered at client
    side). If I misunderstand you, please feel free to tell me.

    ============================================================
    Actually, no matter postback or not, the CreateChildControls method will be
    called. For composite control, the load and maintenance of postback data is
    not due to the composite control, but by its child controls themselves,
    such as TextBox.
    The TextBox control implement the IPostBackDataHandler interface, which
    defines methods that ASP.NET server controls must implement to
    automatically load post back data.

    There are 2 methods in IPostBackDataHandler interface: LoadPostData and
    RaisePostDataChangedEvent.
    1). LoadPostData is used to processes post back data for an ASP.NET server
    control.
    2). RaisePostDataChangedEvent is used to signal the server control object
    to notify the ASP.NET application that the state of the control has changed.

    There is a postback data handling sample in the document, please refer to:
    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/
    frlrfsystemwebuiipostbackdatahandlerclasstopic.asp

    In Control Execution Lifecycle, LoadPostData will be called after the
    CreateChildControls method. In the document sample , you will see that: in
    LoadPostData method, it will judge if the textbox's value has been changed.
    If the value changed, it will retrieve the new value(Which will overwrite
    the default value that being set in CreateChildControls method).

    I think this fully explains the process of the postback data handling.

    Note: Only the data related server side control need to implement this
    IPostBackDataHandler interface.

    ============================================================
    Hope I explains it clear. If you have anything unclear, please feel free to
    tell me.
    Have a nice day!!

    Best regards,
    Jeffrey Tan
    Microsoft Online Partner Support
    Get Secure! - www.microsoft.com/security
    This posting is provided "as is" with no warranties and confers no rights.
    Jeffrey Tan[MSFT], Feb 8, 2004
    #6
  7. paul reed

    Paul Reed Guest

    Jeffrey,

    Thanks so much, this last piece of information is what clarified
    everything for me.

    Best wishes....Paul

    Paul Reed
    www.jacksonreed.com

    *** Sent via Developersdex http://www.developersdex.com ***
    Don't just participate in USENET...get rewarded for it!
    Paul Reed, Feb 8, 2004
    #7
  8. Hi Paul,

    Thanks for your feedback. I am glad I can help you :)

    The Asp.net lifecycle model is important for server side programming. I am
    glad I have clarified it.

    If you have further concern, please feel free to post, I will work with you.

    Have a nice day!!

    Best regards,
    Jeffrey Tan
    Microsoft Online Partner Support
    Get Secure! - www.microsoft.com/security
    This posting is provided "as is" with no warranties and confers no rights.
    Jeffrey Tan[MSFT], Feb 9, 2004
    #8
    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. Mad Scientist Jr
    Replies:
    7
    Views:
    1,158
    Emma Gumbdough
    Jun 30, 2004
  2. bruce barker

    Re: NGen -- SPEED UP DLL INITIAL LOAD

    bruce barker, Jun 30, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    402
    bruce barker
    Jun 30, 2004
  3. John Bonds
    Replies:
    5
    Views:
    3,755
    John Bonds
    Aug 4, 2004
  4. Karim Ali
    Replies:
    6
    Views:
    275
    Diez B. Roggisch
    May 30, 2007
  5. Craig

    CreateChildControls and Exception Failed to Load ViewState

    Craig, Feb 15, 2005, in forum: ASP .Net Building Controls
    Replies:
    2
    Views:
    193
    Craig
    Feb 16, 2005
Loading...

Share This Page