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.
Related
Can anyone please tell me the order of events execution in Asp.Net
Taken from http://msdn.microsoft.com/en-us/library/vstudio/ms178472(v=vs.100).aspx
PreInit
Raised after the start stage is complete and before the initialization stage begins.
Use this event for the following:
Check the IsPostBack property to determine whether this is the first time the page is being processed. The IsCallback and IsCrossPagePostBack properties have also been set at this time.
Create or re-create dynamic controls.
Set a master page dynamically.
Set the Theme property dynamically.
Read or set profile property values.
NoteNote If the request is a postback, the values of the controls have not yet been restored from view state. If you set a control property at this stage, its value might be overwritten in the next event.
Init
Raised after all controls have been initialized and any skin settings have been applied. The Init event of individual controls occurs before the Init event of the page.
Use this event to read or initialize control properties.
InitComplete
Raised at the end of the page's initialization stage. Only one operation takes place between the Init and InitComplete events: tracking of view state changes is turned on. View state tracking enables controls to persist any values that are programmatically added to the ViewState collection. Until view state tracking is turned on, any values added to view state are lost across postbacks. Controls typically turn on view state tracking immediately after they raise their Init event.
Use this event to make changes to view state that you want to make sure are persisted after the next postback.
PreLoad
Raised after the page loads view state for itself and all controls, and after it processes postback data that is included with the Request instance.
Load
The Page object calls the OnLoad method on the Page object, and then recursively does the same for each child control until the page and all controls are loaded. The Load event of individual controls occurs after the Load event of the page.
Use the OnLoad event method to set properties in controls and to establish database connections.
Control events
Use these events to handle specific control events, such as a Button control's Click event or a TextBox control's TextChanged event.
Note In a postback request, if the page contains validator controls, check the IsValid property of the Page and of individual validation controls before performing any processing.
LoadComplete
Raised at the end of the event-handling stage.
Use this event for tasks that require that all other controls on the page be loaded.
PreRender
Raised after the Page object has created all controls that are required in order to render the page, including child controls of composite controls. (To do this, the Page object calls EnsureChildControls for each control and for the page.)
The Page object raises the PreRender event on the Page object, and then recursively does the same for each child control. The PreRender event of individual controls occurs after the PreRender event of the page.
Use the event to make final changes to the contents of the page or its controls before the rendering stage begins.
PreRenderComplete
Raised after each data bound control whose DataSourceID property is set calls its DataBind method. For more information, see Data Binding Events for Data-Bound Controls later in this topic.
SaveStateComplete
Raised after view state and control state have been saved for the page and for all controls. Any changes to the page or controls at this point affect rendering, but the changes will not be retrieved on the next postback.
Render
This is not an event; instead, at this stage of processing, the Page object calls this method on each control. All ASP.NET Web server controls have a Render method that writes out the control's markup to send to the browser.
If you create a custom control, you typically override this method to output the control's markup. However, if your custom control incorporates only standard ASP.NET Web server controls and no custom markup, you do not need to override the Render method. For more information, see Developing Custom ASP.NET Server Controls.
A user control (an .ascx file) automatically incorporates rendering, so you do not need to explicitly render the control in code.
Unload
Raised for each control and then for the page.
In controls, use this event to do final cleanup for specific controls, such as closing control-specific database connections.
For the page itself, use this event to do final cleanup work, such as closing open files and database connections, or finishing up logging or other request-specific tasks.
Note During the unload stage, the page and its controls have been rendered, so you cannot make further changes to the response stream. If you attempt to call a method such as the Response.Write method, the page will throw an exception.
In an interview question today, I was asked which is the last event in page life cycle where you can set the control's properties. I said it will be the event (eg. button click etc) or if there is no event then set it in page load event. Then he said I want the last event name.
Which is the last event to update control's properties and why would one use such an event? and not page load or button click etc?
I would say its the PreRenderComplete event, since the page is rendered immediately afterwards
Although looking at the MSDN page lifecyle, the SaveStateComplete event happens even after that. Though if you were to use that you wouldn't be able to save the control's new properties to the ViewState.
In practice personally I would use the PreRender event to do 'final' control changes.
SaveStateComplete is the last Page-based event where you can set Control properties. However, SaveStateComplete is not available in Controls themselves, and any properties set there will not be persisted in ViewState. Since the original question related to the "Page lifecycle," I think this would be the correct answer.
PreRenderComplete is the last Page-based event where you can set Control properties and have them still persisted in ViewState. As with SaveStateComplete, though, it's not available in Controls.
PreRender is the last event that's available in both Controls and on the Page where you can set properties and have them persisted in ViewState.
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...
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.
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.