Link to home
Start Free TrialLog in
Avatar of josephdaviskcrm
josephdaviskcrmFlag for United States of America

asked on

ASP.NET - Page_PreLoad() and Page_InitComplete() events for a user control

I've got a situation where I need the code in a user control to run before the page it is sitting in.  To do this I am trying to use a method other than Page_Load for the user control so the code is called and runs first.  My first attempt was to use

Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
End Sub

This of course did run the code of the user control before the code on the page, however, in the code for the user control I am creating many controls dynamically and adding them to panels.  It seems that everything that is supposed to be added dynamically is missing when the code executes in the Page_Init() method.

Doing a little more research, I found the methods Page_InitComplete() and Page_PreLoad.  But neither one of these seems to work for user controls.

Any suggestions as to how I might tackle this problem?
Avatar of ppittle
ppittle
Flag of United States of America image

Try overriding the CreateChildControls method on the UserControl.  This method is called by the Page Lifecycle.   If you need to perform work on the UserControl's child controls from a Parent Control or Parent Page early on in the Lifecycle (ie Page_Load), call the UserControl's EnsureChildControls() method first.

Hope that helps,
PJ
Avatar of josephdaviskcrm

ASKER

The EnsureChildControls() method looks interesting.  Can you tell me a little more about it and perhaps show me a code example of how it is used?

What I've tried so far is just to put EnsureChildControls() at the top of my pages page_Load() method.  But it isn't making any difference.

Maybe the better solution would be to override the CreateChildControls method on the UserControl as you mentioned first.  But I also need a little more direction on how that works as well if you can.

Thanks.
ASKER CERTIFIED SOLUTION
Avatar of ppittle
ppittle
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial