简体   繁体   English

我们可以在页面生命周期中的什么时候分配母版页?

[英]When in the page lifecycle we can assign master page?

I know ViewState is available between InitComplete and Preload events in LoadViewSate method.我知道 ViewState 在 LoadViewSate 方法中的 InitComplete 和 Preload 事件之间可用。 Similarly, I want to know in which page lifecycle event can we assign a master page for a particular 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通常,您在 PreInit 阶段动态分配母版页

On Page PreInit event On Page PreInit事件

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

Read Working with ASP.NET Master Pages Programmatically阅读以编程方式使用 ASP.NET 母版页

From: ASP.NET Page Life Cycle Overview来自: ASP.NET 页面生命周期概述

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.检查 IsPostBack 属性以确定这是否是第一次处理页面。 The IsCallback and IsCrossPagePostBack properties have also been set at this time.此时还设置了 IsCallback 和 IsCrossPagePostBack 属性。

  • Create or re-create dynamic controls.创建或重新创建动态控件。
  • Set a master page dynamically.动态设置母版页。
  • Set the Theme property dynamically.动态设置 Theme 属性。
  • 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.除了以声明方式(在@ Page directive or in the configuration file )指定母版页之外,您还可以将母版页动态附加到内容页。 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:通常,您在PreInit阶段动态分配母版页,如下例所示:

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

Edit:编辑:

Source: ASP.NET Master Pages - How Master Pages Work来源: ASP.NET 母版页 - 母版页的工作原理
You can use @Page directive also to specify master page.您也可以使用 @Page 指令来指定母版页。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM