Domanda

I know ViewState is available between InitComplete and Preload events in LoadViewSate method. Similarly, I want to know in which page lifecycle event can we assign a master page for a particular page?

È stato utile?

Soluzione

Because the master page and content page are merged during the initialization stage of page processing, a master page must be assigned before then. Typically, you assign a master page dynamically during the PreInit stage

On Page PreInit event

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

Read Working with ASP.NET Master Pages Programmatically

Altri suggerimenti

From: ASP.NET Page Life Cycle Overview

Page Event

Typical Use

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.

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

From: Attaching Master Pages Dynamically

In addition to specifying a master page declaratively (in the @ Page directive or in the configuration file), you can attach a master page dynamically to a content page. Because the master page and content page are merged during the initialization stage of page processing, a master page must be assigned before then. Typically, you assign a master page dynamically during the PreInit stage, 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 use @Page directive also to specify master page.

<% @ Page Language="C#" MasterPageFile="~/Master.master" Title="Content Page 1" %>
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top