I encountered a problem today. I added a judgment to the onload of masterpage. I want to execute the judgment before the onload of contentpage. The result shows that the onload of masterpage is executed after the onload of contentpage, later, I moved this judgment to the oninit of masterpage.
In a separate page, the execution order is
- Preinit
- Init
- Initcomplete
- Preload
- Load
- Loadcomplete
- Prerender
- Prerendercomplete
When masterpage is available on the page
- Contentpage. preinit
- Master. init
- Contentpage. init
- Contentpage. initcomplete
- Contentpage. Preload
- Contentpage. Load
- Master. Load
- Contentpage. loadcomplete
- Contentpage. prerender
- Master. prerender
Note that the highlighted part above is usually masterpage followed by contentpage.
So what is the relationship between widget loading and several page events?
Generally, the control on the page is executed before oninit, that is
- Init of the control in the master
- Init of the control in contentpage
- Master. init
- Content. init
- Content. Load
- Master. Load
- Master. User Control Load
- Page_load of the user control on the contentpage page
You can see that the control init is executed before the page init, so we can safely use the control in the page in init, but the following situations are different.
If your page uses webcontrol or controls inherited from webcontrol, webcontrol needs to use createchildcontrols () to load child controls. This function will be called after init of the control, that is to say, in oninit of a webcontrol, there is no way to directly use the control. However, we can also use findcontrol to load the control. The following table lists the loading sequence of other webcontrol methods.
| Event |
Description |
| Init |
You can use the oninit method to trap this event And initialize member variables and other values. |
| Loadviewstate |
You can trap this event to customize how Control retrieves information from the ASP. NET hidden viewstate field. |
| Load |
This event is raised after you create and Initialize the control. This is the best place to make the connection to Database or load document content. you can trap the load event by adding Onload Method to the class. |
| Prerender |
This event is raised before ASP. NET renders Control. Any changes to the control's state are saved into the hidden viewstate Field. |
| Saveviewstate |
This event is raised before the control state is Persisted into the hidden viewstate field. You can trap the event to customize How this information is stored. |
| Render |
You can use the render method to respond to this Event and specify the HTML code that represents the component's Content. |
| Dispose |
This event is useful for cleanup operations. It's Raised before the control is torn down and is the best place to free Resources created during the load phase. |
| Unload |
This event is raised before the control is torn Down. The official documentation says not to use this event to perform cleanup, And to rely on the dispose event Instead. |