I have a UserControl inheriting from another, and my question is simple. Does the control's page load fire first, or does the base class page load fire first?
"The Page calls the OnLoad event method on the Page, then recursively does the same for each child control, which does the same for each of its child controls until the page and all controls are loaded."
From this link: http://msdn.microsoft.com/en-us/library/ms178472.aspx
So to answer plainly, the Page_Load event is called before the load event in user controls
If you are talking about an actual inheritance, and not control composition, then all the standard Object Oriented rules apply.
Because these are not separate object, there is only one Load event on your control, and there can be only one Page_Load method, unless you explicitly hide it using the new modifier. So, in essence, there is no difference between the Child page load, and the Parent page load... they are one in the same.
The control's Page_Load should fire first, I believe. Other than the Page_Init event, all other initiation events occur up the control hierarchy.
Edit: I'm wrong up there. The page fires its load event then recursively calls it on child controls, which recursively call it on its child controls, and so on. My bad...
Related
I've seen code to handle MasterPage events in the content Page, but if I'm loading a UserControl dynamically into the Page, can I handle the event in the UserControl instead?
Basically I have a button on the MasterPage, when it's clicked I need to make the UserControl do something, such as display text or change a value in a form.
Failing that, is it possible to make an event from one dynamically loaded UserControl fire in another on the same page? I could then replace the button in the MasterPage with one in another UserControl
Thanks all.
This is very similar to this stackoverflow question.
The answer to that question should also apply to yours: your Page knows its master and knows its child controls. It can wire-up a handler on the child control to an event on the master page, so the child control can perform necessary functionality when the even occurs on the master page.
which is the earliest event when child control exists ?
In page lifecycle you can use OnInit as the earliest event.
but if you mean the earliest event that you can access the child controls,
Call EnsureChildControls method at the beginning of the event, then you can access the child controls of the control. If child controls are not initiated, it simply calls the CreateChildControls methods, and initiates them.
Control_Init event.
From the ASP.Net Page Lifecycle article on MSDN:
Although both Init and Load recursively occur on each control, they happen in reverse order. The Init event (and also the Unload event) for each child control occur before the corresponding event is raised for its container (bottom-up). However the Load event for a container occurs before the Load events for its child controls (top-down).
This makes a lot of sense for the unload event, but why for init? What about the other events?
Kind regards,
This is due to how these pages are created - the user controls are properties of the page's class. They are created (and hence initialised) as the class is initialised, which ensures that their instances are available during the constructor and page's init event.
Then when load, prerender and render events occur the page's event fires first and cascades the events for everything inside it.
When the unload and dispose come around the property objects are dealt with first again.
The WebForm page event model is a little too complicated, IMHO.
Is there any kind of event out there that would allow for a preload post back event.
The reason I ask is I have a control that adds sibling controls to it on postback events, however, by the time it has loaded the post back its too late to add the new control to the control collection. Therefore, the controls are never updated correctly.
Thanks!
Try the Init event.
Override CreateChildControls (make sure to call base!). In your postback event handler, make sure you are storing somewhere the list of controls that should be created dynamically, so when CreateChildControls gets invoked very early in the lifecycle on the next go-round, it will recreate the controls built on the last postback.
Here is a quick hack. You can always, query the __EventTarget and or the value of the submit button in init and can load dynamically the control.
But doing so, may not be appropriate as your control hierarchy would change and could cause problems.
As above, dynamic controls have to be added during the page Init event, so that they can be properly handled within the page's Viewstate. You might want to turn the Viewstate off for the page as well, since it can fire errors at you if the controls change.
As has already been stated the proper place to add dynamic controls is in the Init event.
Here's an article with more information.
Dynamic Web Controls, Postbacks, and View State
To get a better understanding of the ASP .NET page life cycle see:
ASP.NET Page Life Cycle Overview
This page explains the event order (and what happens in each one) in a postback, it helped me more than once.
I've just found this link, that can also be of use to you
I have a MasterPage, with my Usercontrol inside a div.
I can set visible=false to the UserControl and to the containing div, and this works fine.
But the Page_Load of the UserControl is always hit.
Is this by design, or am I missing how to stop page execution going into the Page_Load method of the UserControl.
When you add a UserControl to a page it will be instantiated and added to the Page.Controls collection every time the page is executed. When you set Visible to false it basically just short circuits the rendering of the control's html so that it doesn't show on the page.
If there is some expensive operation that your control does in "Page_Load" I would key off of the Visible property to circumvent that operation.
If the control was never created, how could you tell it to be invisible?
You cant stop execution going into the PageLoad event method for a user control.
#joshperry suggestion to use Visible property to determine whether or not to do a time consuming operation in the code is a good one.
The other option would be to complete the time consuming option in the OnPreRender event method.