When can we designate a homepage in the page life cycle?

This question was asked to me recently by my colleague. I know that ViewState is available between the InitComplete and Preload events in the LoadViewSate method. Similarly, I want to know in which page life cycle event we can assign a home page for a specific page?

+4
source share
2 answers

Since the main page and the content page are combined during the initialization stage of the page processing, the main page must be assigned before that. Usually you assign the main page dynamically during the PreInit phase

In Page PreInit event

 void Page_PreInit(Object sender, EventArgs e) { this.MasterPageFile = "~/MyMaster.master"; } 

Read Working with ASP.NET Core Pages Programmatically

+7
source

Refer: ASP.NET Page Life Cycle Overview

Page Event Typical Usage PreInit Restores after the start of the start phase and before the start of the initialization phase. Use this event for the following: Check the IsPostBack property to determine if this will be the first time the page is being processed. The IsCallback and IsCrossPagePostBack properties were also set at this time.

  • Create or recreate dynamic controls.
  • Dynamic installation of the main page.
  • Set the Theme property dynamically.
  • Read or set the value profile property.

Note If the request is a postback, the control values ​​have not yet been restored from the view state. If you set a control property at this point, its value may be overwritten in the next event.

From: Dynamic connection of main pages

In addition to specifying the main page declaratively (in the @ Page directive or in the configuration file ), you can dynamically attach the main page to the content page. Since the main page and the content page are combined at the initialization stage of the page processing, the main page must be assigned before this. Usually you assign the main page dynamically during PreInit , as in the following example:

 void Page_PreInit(Object sender, EventArgs e) { this.MasterPageFile = "~/DefaultMaster.master"; } 

Edit:

Source: ASP.NET Master Pages - How Master Pages Work
You can also use the @Page directive to specify the main page.

 <% @ Page Language="C#" MasterPageFile="~/Master.master" Title="Content Page 1" %> 
+3
source

All Articles