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.

Master page life cycle

While serving an ASP.NET request, it goes through many stages. One of the important stages is going through the page life cycle events. A page is a combination of master and content pages and may contain controls like user controls and custom controls. Here, each control has its own life cycle. So, if the controls are interleaved, it becomes necessary to look into the exact event which is being triggered.

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

  1. 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 the Init() 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 the Page_PreInit() method, neither the master page nor any control is initialized.

  2. 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 the LoadPostData() event fires. We can access control values from Form’s Post data.

  3. PreLoad() Event: This is used if you need to perform processing on your page or control before the Load() event. It loads view state for itself and all controls. It also processes any postback data included with the request instance.

  4. 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 the IsPostBack() property to determine whether the page is loaded for the first time or in response to client postback.

  5. Control Event handlers: These are event handlers like Button1_Click() which are defined for controls in the ASPX markup.

  6. 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.

  7. PreRender() Event: This event is recursively fired for each child control on the page.

  8. 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.

  9. Render() Event: The Page object calls this method on each control. All ASP.NET web controls have a render method, which, after writing, the control’s markup is sent to the browser.

  10. 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 1200+ tech skills courses.