datasource of child .ascx not visible to parent .ascx

J

Joe

//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
 
J

Jeffrey Tan[MSFT]

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.
 

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

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,045
Latest member
DRCM

Latest Threads

Top