When does LoadControl really load the control?

R

Rory Plaire

Hi,

I'm loading a user control (.ascx) with LoadControl in a code-behind
page. LoadControl returns successfully, and the valid reference it
returns is successfully cast to the type of control I need
(TimeEntryGrid). All this is done in Page_Init so that the control can
catch the Page_Load the first time around.

On the first time the page is loaded, I don't actually add the derived
user control to the page control hierarchy, since it is not needed
until the user requests it.

The problem with this: it appears that none of the lifecycle events
are fired for the user control... not even Init. When I add the
control to the Page control hierarchy through a
Control.Controls.Add(), the events are fired as expected.

Why doesn't LoadControl actually Load the Control?

thanks,
-rory 8)
 
J

John Saunders

Rory Plaire said:
Hi,

I'm loading a user control (.ascx) with LoadControl in a code-behind
page. LoadControl returns successfully, and the valid reference it
returns is successfully cast to the type of control I need
(TimeEntryGrid). All this is done in Page_Init so that the control can
catch the Page_Load the first time around.

On the first time the page is loaded, I don't actually add the derived
user control to the page control hierarchy, since it is not needed
until the user requests it.

The problem with this: it appears that none of the lifecycle events
are fired for the user control... not even Init. When I add the
control to the Page control hierarchy through a
Control.Controls.Add(), the events are fired as expected.

Why doesn't LoadControl actually Load the Control?

LoadControl did load the control. You just need to add it to the control
hierarchy.

If it's not in the hierarchy, how is ASP.NET going to find it to tell it,
for instance, to do a Load?
 
R

Rory Plaire

John Saunders said:
LoadControl did load the control. You just need to add it to the control
hierarchy.

If it's not in the hierarchy, how is ASP.NET going to find it to tell it,
for instance, to do a Load?

Thanks for the reply!

You know, I thought this same thought when I was driving home
yesterday. I guess that Page.ProcessRequest() does a recursive walk of
the component tree and invokes methods on it based on postback data.

Thanks for confirming...

-rory 8)
 

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,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top