datasource of child .ascx not visible to parent .ascx

Discussion in 'ASP .Net Web Controls' started by Joe, Feb 13, 2004.

  1. Joe

    Joe Guest

    //Parent ascx..
    Control ChildControl = Page.LoadControl(Request.ApplicationPath + "/ChildControlSourceCode.ascx");
    Page.WhatEverContainer.Controls.Add(ChildControl

    //Now this in ChildControl Page_Load is fired..
    DataGrid1.DataSource = ValidVerifiedDataTableWithRowsEverythingAOK
    DataGrid1.DataBind()

    //Back to Paren
    DataGrid dg = (DataGrid)ChildControl.FindControl("DataGrid1")
    object XportTable = dg.DataSource

    ***dg.DataSource is null. If the datagrid code is moved to the OnInit event in the child, then the parent can "see" the datasource and access it - WHY

    Jo
    Joe, Feb 13, 2004
    #1
    1. Advertising

  2. Hi Joe,

    Thank you for posting in the community!

    Based on my understanding, you dynamic load a usercontrol in your webpage.
    There is a datagrid in the usercontrol, and you databind the datagrid in
    usercontrol's Page_Load event. But in parent's Load event, when you access
    the datagrid.datasource, it is a null reference.

    =============================================
    Where do you dynamicly load the usercontrol? I suppose you load it in
    Parent's Load event.

    Actually, this behavior is by the design of Web Form's LifeCycle.

    In Asp.net web form model, child control's Load event will fire after the
    Parent's Load event. So, when you added the usercontrol to the page, in
    page's load event, the datagrid has not been bound, the dg.DataSource will
    return null reference.

    Also, in the LifeCycle for dynamicly added control, it will continue all
    its execute process until the Parent page's process. That is, in your code,
    the event order will be:

    ~~~~~~~~~~~~~~~~~~~~~~~~
    Parent.Init

    Parent.Load:
    Control ChildControl = Page.LoadControl(Request.ApplicationPath +
    "/ChildControlSourceCode.ascx");
    Page.WhatEverContainer.Controls.Add(ChildControl)

    //usercontrol is added, its will execute until the Load event.
    Usercontrol.Init

    Parent.Load(CONINUE execute):
    DataGrid dg = (DataGrid)ChildControl.FindControl("DataGrid1");
    object XportTable = dg.DataSource;

    Usercontrol.Load
    ~~~~~~~~~~~~~~~~~~~~~~~~

    In the event order, you will see that, the Page.Load event is divided into
    2 parts(separate by the Controls.Add statement).

    So, if you place the DataGrid databinding code in usercontrol.Init event,
    then you can get the correct datasource. While place it in the
    usercontrol.Load event, object XportTable = dg.DataSource will execute
    before the databinding statement.(Null reference will return.)

    Hope I have clarified it.

    =============================================
    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.
    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 16, 2004
    #2
    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. spamfurnace
    Replies:
    1
    Views:
    2,626
    Scott M.
    May 24, 2004
  2. Replies:
    0
    Views:
    195
  3. Jurjen de Groot
    Replies:
    0
    Views:
    297
    Jurjen de Groot
    May 19, 2008
  4. Noel Dolan
    Replies:
    0
    Views:
    217
    Noel Dolan
    Jul 18, 2004
  5. Bitswapper
    Replies:
    5
    Views:
    119
    Prasad, Ramit
    Aug 27, 2013
Loading...

Share This Page