MasterPage 类

.NET Framework 类库 
MasterPage 类 

注意:此类在 .NET Framework 2.0 版中是新增的。

作为页的模板和合并容器,这些页仅由 Content 控件和它们各自的子控件构成。

命名空间:System.Web.UI
程序集:System.Web(在 system.web.dll 中)

语法
Visual Basic(声明)
Public Class MasterPage
Inherits UserControl
Visual Basic(用法)
Dim instance As MasterPage
C#
public class MasterPage : UserControl
C++
public ref class MasterPage : public UserControl
J#
public class MasterPage extends UserControl
JScript
public class MasterPage extends UserControl
备注

母版页用作 ASP.NET Web 应用程序中内容页的模板容器和合并页。母版页为在一组内容页之间共享结构和内容提供了一条方便的途径。可使用内容占位符定义母版页中要用内容页的内容加以替换的部分。

当您使用母版页及其相关内容页时,您将所需的 XHTML 文档标记(如 htmlheadbody)仅添加到母版页,而无需创建作为独立网页的其他 .aspx 文件(ASP.NET 页)。内容页定义要插入到母版页占位符中的内容。

在运行时,如果对一个页发出 HTTP 请求,母版页和内容页将被组合为与内容页同名的单个类。而最终经过编译和合并的类从 Page 类派生。

母版页可包含直接标记和服务器控件,以及容器控件。母版页上处于 ContentPlaceHolder 控件以外的每个元素会显示在由母版页和内容页合并所产生的所有页上。

每个与母版页相关的内容页必须在其 @ Page 指令的 MasterPageFile 属性中引用母版页。内容页只能包含该 @ Page 指令和一个或多个 Content 控件。所有页文本、标记和服务器控件都必须放置在 Content 控件中。通过设置 Content 控件的 ContentPlaceHolderID 属性,可标识与 Content 控件关联的母版页的 ContentPlaceHolder 控件。

在运行时,所请求页上的每个 Content 控件的动态内容将合并到母版页上与这些控件关联的 ContentPlaceHolder 控件的确切位置。母版页中所有其他标记和控件不受影响。在母版类中和内容页上,都可存在事件处理程序。有关更多信息,请参见 ASP.NET 母版页和内容页中的事件

MasterPage 类与扩展名为 .master 的文件相关联。这些文件在运行时被编译为 MasterPage 对象,并被缓存在服务器内存中。

通过基 Page 类的 Master 属性,内容页可以使用母版页。Master 属性返回母版页的实例;但是,它的类型为基 MasterPage 类。若要访问母版页的控件、属性和函数,Master 属性可强制转换为 MasterPage。母版页的类名用 @ Master 指令的 ClassName 属性定义。

Note注意

不会向浏览器提供扩展名为 .master 的文件。

母版页上的有效指令与 UserControl 对象上的可用指令相同,它们可包括以下属性:

  • AutoEventWireup

  • ClassName

  • CodeFile

  • CompilerMode

  • CompilerOptions

  • Debug

  • Description

  • EnableTheming

  • EnableViewState

  • Explicit

  • Inherits

  • Language

  • LinePragmas

  • MasterPageFile

  • Src

  • Strict

  • WarningLevel

母版页指令不会重写各个内容页上的指令。

母版页大部分情况下以声明方式创建。如果您希望以编程方式创建母版页,请直接从 MasterPage 类派生。除了扩展 MasterPage 类以外,您必须创建 .master 文件,从而以视觉方式显示与您在源文件中所调用类相关联的用户界面 (UI)。

Note注意

当您通过首先创建自己的类来创建母版页时,您必须包括该页所使用的类需要的全部命名空间。

有关母版页的更多信息,请参见 ASP.NET 母版页概述

示例

本节包含四个代码示例:

  • 第一个代码示例演示如何以声明方式创建母版页。

  • 第二个代码示例表示与第一个代码示例中所创建的母版页相关联的内容页。

  • 第三个代码示例演示如何向母版页添加属性。

  • 第四个代码示例演示如何使用内容页来访问母版页上的公共属性。

下面的代码示例演示如何以声明方式创建母版页,以及如何使用内容页向它添加一些内容。第一个网页是名为 MasterPageSample_1.master 的 .master 页。

<%@ Master Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html  >
<head runat="server">
<title>MasterPage Example</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:contentplaceholder id="ContentPlaceHolder1" runat="server" />
</div>
</form>
</body>
</html>
Visual Basic
<%@ Master Language="VB" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html  >
<head runat="server">
<title>MasterPage Example</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:contentplaceholder id="ContentPlaceHolder1" runat="server" />
</div>
</form>
</body>
</html>

下面的代码示例表示与 MasterPageSample_1.master 相关联的内容页。它包含一个 Content 控件,并使用 ContentPlaceHolderID 属性标识与内容相关联的 ContentPlaceHolder 控件。

<%@ Page Language="C#" MasterPageFile="~/MasterPageSample_1cs.master" Title="Content Page"%>
<asp:content
runat="server"
contentplaceholderid="ContentPlaceHolder1" >Hello, Master Pages!</asp:content>
Visual Basic
<%@ Page Language="VB" MasterPageFile="~/MasterPageSample_1vb.master" Title="Content Page"%>
<asp:content
runat="server"
contentplaceholderid="ContentPlaceHolder1" >Hello, Master Pages!</asp:content>

下面的代码示例演示如何向母版页添加属性。ClassName 属性用于为母版页命名。

<%@ Master Language="C#" ClassName="MasterExample" %>
<script runat="server">
public string SiteName
{
get { return "My Site Name"; }
}
</script>
<html  >
<head runat="server">
<title>MasterPage Example</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:contentplaceholder id="ContentPlaceHolder1" runat="server">
</asp:contentplaceholder>
</div>
</form>
</body>
</html>
Visual Basic
<%@ Master Language="VB" ClassName="MasterExample" %>
<script runat="server">
Public ReadOnly Property SiteName() As String
Get
Return "My Site Name"
End Get
End Property
</script>
<html  >
<head runat="server">
<title>MasterPage Example</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:contentplaceholder id="ContentPlaceHolder1" runat="server">
</asp:contentplaceholder>
</div>
</form>
</body>
</html>

下面的代码示例演示如何使用内容页访问前一个代码示例中的母版页上的公共属性 SiteName

<%@ Page Language="C#" MasterPageFile="~/MasterPage.master" Title="MasterPage Example" %>
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
MasterExample m = (MasterExample)Page.Master;
mylabel.Text = m.SiteName;
}
</script>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
Hello, Master Pages!
<asp:Label runat="server" Text="Label" ID="mylabel"></asp:Label>
</asp:Content>
Visual Basic
<%@ Page Language="VB" MasterPageFile="~/MasterPage.master" Title="MasterPage Example" %>
<script runat="server">
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
Dim m As MasterExample = CType(Page.Master, MasterPage)
mylabel.Text = m.SiteName
End Sub
</script>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
Hello, Master Pages!
<asp:Label runat="server" Text="Label" ID="mylabel"></asp:Label>
</asp:Content>
.NET Framework 安全性
继承层次结构
System.Object
   System.Web.UI.Control
     System.Web.UI.TemplateControl
       System.Web.UI.UserControl
        System.Web.UI.MasterPage
线程安全
此类型的任何公共静态(Visual Basic 中的 Shared)成员都是线程安全的,但不保证所有实例成员都是线程安全的。
平台

Windows 98、Windows 2000 SP4、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0
我来自:向东博客
原文地址:https://www.cnblogs.com/meil/p/435557.html