Master Page Life Cycle Events
In this lesson, you will learn about the page life cycle, which is very crucial in order to develop ASP.NET applications. Most beginners tend to get confused while dealing with 'dynamic controls' and face problems like losing value, state, etc. on postbacks. The sequence of events, especially while working with master pages in ASP.NET has become more complex. This lesson aims to shed light on these events.
We'll cover the following
Master page life cycle
While serving an
Technically, a master page is nothing but a normal user control. It can be treated as a user control on a web page. The content page is nothing but a normal ASPX page.
Events in the master page life cycle
-
PreInit()
Event: In this page-level event, all controls created during design time are initiated with their default values. Dynamic controls are created here. But, this event occurs for page class only. User controls and the master page do not have this method to override. This is also the event where themes can be set programmatically.If a master page is associated with the page, the controls on the page will not be initiated and would be
null
. After theInit()
event, the controls can be accessed directly from the Page class. Here, all controls are placed in a content Placeholder, which is a child control of a master page. The master page is merged and treated as a control in the content page. In thePage_PreInit()
method, neither the master page nor any control is initialized. -
OnInit()
Event: Here, the control’s properties are read or set at design time. We cannot read control values changed by the user because the changed value gets loaded after theLoadPostData()
event fires. We can access control values from Form’s Post data. -
PreLoad()
Event: This is used if you need to perform processing on your page or control before theLoad()
event. It loads view state for itself and all controls. It also processes any postback data included with the request instance. -
Load()
Event: At this stage in the page lifecycle the server controls in the hierarchy are created and initialized, view-state is restored, and controls reflect client-side data. Use theIsPostBack()
property to determine whether the page is loaded for the first time or in response to client postback. -
Control Event handlers: These are event handlers like
Button1_Click()
which are defined for controls in the ASPX markup. -
OnLoadComplete()
Event: This event occurs after all postback data and view state data is loaded into the page as well as the controls on the page. -
PreRender()
Event: This event is recursively fired for each child control on the page. -
Save State Complete Event: Before this event, View State has been saved for the page and all controls. Any changes to the page or controls after this point are ignored.
-
Render()
Event: The Page object calls this method on each control. AllASP.NET web controls have a render method, which, after writing, the control’s markup is sent to the browser. -
Unload()
Event: This event occurs for Page as well as each control. For controls, use this event to do final cleanup for specific controls, such as closing control-specific database connections.
Get hands-on with 1400+ tech skills courses.