MasterPage 类
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
充当仅由 Content 控件和它们各自的子控件组成的页面的模板和合并容器。
public ref class MasterPage : System::Web::UI::UserControl
public class MasterPage : System.Web.UI.UserControl
type MasterPage = class
inherit UserControl
Public Class MasterPage
Inherits UserControl
- 继承
示例
本部分包含四个代码示例:
第一个代码示例演示如何以声明方式创建母版页。
第二个代码示例表示与在第一个代码示例中创建的母版页关联的内容页。
第三个代码示例演示如何将属性添加到母版页。
第四个代码示例演示如何使用内容页访问母版页上的公共属性。
以下示例演示如何以声明方式创建母版页,以及如何使用内容页向其中添加一些内容。 第一个网页是名为 的 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>
<%@ 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 控件,并使用 属性标识 ContentPlaceHolder 与内容关联的控件 ContentPlaceHolderID 。
<%@ Page Language="C#" MasterPageFile="~/MasterPageSample_1cs.master" Title="Content Page"%>
<asp:content
runat="server"
contentplaceholderid="ContentPlaceHolder1" >Hello, Master Pages!</asp:content>
<%@ 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>
<%@ 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>
<%@ 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>
注解
母版页充当 ASP.NET Web 应用程序中的内容页的模板容器和合并页。 母版页提供了一种跨一组内容页共享结构和内容的便捷方法。 使用内容占位符定义母版页的各个部分,以替换为内容页中的内容。
使用母版页及其相关内容页时,仅将所需的 XHTML 文档标记 ((如 html、 head和 body) )添加到母版页,不再创建其他.aspx文件 (ASP.NET 页) 为独立网页。 内容页定义要插入母版页中的占位符的内容。
在运行时对页面发出 HTTP 请求时,母版页和内容页将合并为与内容页同名的单个类。 生成的已编译合并类派生自 Page 类。
母版页可以包含直接标记和服务器控件以及容器控件。 放置在控件外部的 ContentPlaceHolder 母版页中的每个元素都呈现在合并母版页和内容页所导致的所有页面上。
与母版页相关的每个内容页都必须在其 @ Page 指令的 属性 MasterPageFile 中引用母 版页 。 内容页只能包含指令 @ Page 和一个或多个 Content 控件。 所有页面文本、标记和服务器控件都必须放置在 控件中 Content 。 通过设置 ContentPlaceHolderID 控件的 属性,Content可以标识ContentPlaceHolder与控件关联的母版页的Content控件。
在运行时,请求页中每个 Content 控件的动态内容将与母版页合并到相关 ContentPlaceHolder 控件的确切位置。 母版页中的任何其他标记和控件不受影响。 可以在母版类和内容页上定义事件处理程序。 有关详细信息,请参阅 ASP.NET 母版页和内容页中的事件。
类 MasterPage 与扩展名为 .master 的文件相关联。 这些文件在运行时编译为 MasterPage 对象,并缓存在服务器内存中。
母版页通过 Master 基 Page 类的 属性提供给内容页。 属性 Master 返回母版页的实例;但是,它被类型化为基 MasterPage 类。 若要访问母版页的控件、属性和函数, Master 可以将 属性强制转换为 MasterPage 对象。 母版页的类名是使用 ClassName@ Master 指令的 属性定义的。
注意
具有 .master 扩展名的文件不会提供给浏览器。
在母版页上有效的指令与对象上 UserControl 可用的指令相同。 它们可包含以下属性:
AutoEventWireupClassNameCodeFileCompilerModeCompilerOptionsDebugDescriptionEnableThemingEnableViewStateExplicitInheritsLanguageLinePragmasMasterPageFileSrcStrictWarningLevel
母版页指令不会覆盖单个内容页上的 指令。
母版页通常是以声明方式创建的。 如果要以编程方式创建母版页,请直接从 MasterPage 类派生。 除了扩展 MasterPage 类之外,还必须创建 .master 文件,以便直观地显示用户界面 (UI) ,该用户界面与在源文件中调用的类相关联。
注意
通过首先创建自己的类来创建母版页时,必须包含页面使用的类所需的所有命名空间。
有关母版页的详细信息,请参阅 ASP.NET 母版页。
构造函数
| MasterPage() |
创建 MasterPage 类的新实例。 |
属性
| Adapter |
获取控件的浏览器特定适配器。 (继承自 Control) |
| Application |
为当前 Web 请求获取 Application 对象。 (继承自 UserControl) |
| AppRelativeTemplateSourceDirectory |
获取或设置包含该控件的 Page 或 UserControl 对象的应用程序相对虚拟目录。 (继承自 Control) |
| AppRelativeVirtualPath |
获取或设置从中解析和编译控件的文件的相对于应用程序的虚拟目录路径。 (继承自 TemplateControl) |
| Attributes |
获取在 .aspx 文件中的用户控件标记中声明的所有特性名和值对的集合。 (继承自 UserControl) |
| AutoHandlers |
已过时.
ASP.NET 2.0 中已弃用 AutoHandlers 属性。 它是由生成的类使用的,不应在代码中使用。 (继承自 TemplateControl) |
| BindingContainer |
获取包含该控件的数据绑定的控件。 (继承自 Control) |
| Cache |
获取与包含用户控件的应用程序关联的 Cache 对象。 (继承自 UserControl) |
| CachePolicy |
获取对该用户控件的缓存参数集合的引用。 (继承自 UserControl) |
| ChildControlsCreated |
获取一个值,该值指示是否已创建服务器控件的子控件。 (继承自 Control) |
| ClientID |
获取由 ASP.NET 生成的 HTML 标记的控件 ID。 (继承自 Control) |
| ClientIDMode |
获取或设置用于生成 ClientID 属性值的算法。 (继承自 Control) |
| ClientIDSeparator |
获取一个字符值,该值表示 ClientID 属性中使用的分隔符字符。 (继承自 Control) |
| ContentPlaceHolders |
获取母版页用来定义不同内容区域的 ContentPlaceHolder 控件的列表。 |
| ContentTemplates |
获取与母版页相关联的内容控件的列表。 |
| Context |
为当前 Web 请求获取与服务器控件关联的 HttpContext 对象。 (继承自 Control) |
| Controls |
获取 ControlCollection 对象,该对象表示 UI 层次结构中的指定服务器控件的子控件。 (继承自 Control) |
| DataItemContainer |
如果命名容器实现 IDataItemContainer,则获取对命名容器的引用。 (继承自 Control) |
| DataKeysContainer |
如果命名容器实现 IDataKeysControl,则获取对命名容器的引用。 (继承自 Control) |
| DesignMode |
获取一个值,该值指示是否正在使用设计图面上的一个控件。 (继承自 Control) |
| EnableTheming |
获取或设置一个布尔值,该值指示主题是否应用于派生自 TemplateControl 类的控件。 (继承自 TemplateControl) |
| EnableViewState |
获取或设置一个值,该值指示服务器控件是否向发出请求的客户端保持自己的视图状态以及它所包含的任何子控件的视图状态。 (继承自 Control) |
| Events |
获取控件的事件处理程序委托列表。 此属性为只读。 (继承自 Control) |
| HasChildViewState |
获取一个值,该值指示当前服务器控件的子控件是否具有任何已保存的视图状态设置。 (继承自 Control) |
| ID |
获取或设置分配给服务器控件的编程标识符。 (继承自 Control) |
| IdSeparator |
获取用于分隔控件标识符的字符。 (继承自 Control) |
| IsChildControlStateCleared |
获取一个值,该值指示该控件中包含的控件是否具有控件状态。 (继承自 Control) |
| IsPostBack |
获取一个值,该值指示是正为响应客户端回发而加载用户控件,还是正第一次加载和访问用户控件。 (继承自 UserControl) |
| IsTrackingViewState |
获取一个值,用于指示服务器控件是否会将更改保存到其视图状态中。 (继承自 Control) |
| IsViewStateEnabled |
获取一个值,该值指示是否为该控件启用了视图状态。 (继承自 Control) |
| LoadViewStateByID |
获取一个值,该值指示控件是否通过 ID 而不是索引参与加载其视图状态。 (继承自 Control) |
| Master |
获取嵌套母版页方案中当前母版页的父级母版页。 |
| MasterPageFile |
获取或设置包含当前内容的母版页的名称。 |
| NamingContainer |
获取对服务器控件的命名容器的引用,此引用创建唯一的命名空间,以区分具有相同 ID 属性值的服务器控件。 (继承自 Control) |
| Page |
获取对包含服务器控件的 Page 实例的引用。 (继承自 Control) |
| Parent |
获取对页 UI 层次结构中服务器控件的父控件的引用。 (继承自 Control) |
| RenderingCompatibility |
获取一个值,该值指定呈现的 HTML 将与之兼容的 ASP.NET 版本。 (继承自 Control) |
| Request |
为当前 Web 请求获取 HttpRequest 对象。 (继承自 UserControl) |
| Response |
为当前 Web 请求获取 HttpResponse 对象。 (继承自 UserControl) |
| Server |
为当前 Web 请求获取 HttpServerUtility 对象。 (继承自 UserControl) |
| Session |
为当前 Web 请求获取 HttpSessionState 对象。 (继承自 UserControl) |
| Site |
获取容器信息,该容器在呈现于设计图面上时承载当前控件。 (继承自 Control) |
| SkinID |
获取或设置要应用于控件的外观。 (继承自 Control) |
| SupportAutoEvents |
获取一个值,该值指示 TemplateControl 控件是否支持自动事件。 (继承自 TemplateControl) |
| TemplateControl |
获取或设置对包含该控件的模板的引用。 (继承自 Control) |
| TemplateSourceDirectory |
获取包含当前服务器控件的 Page 或 UserControl 的虚拟目录。 (继承自 Control) |
| Trace |
为当前 Web 请求获取 TraceContext 对象。 (继承自 UserControl) |
| UniqueID |
获取服务器控件的唯一的、以分层形式限定的标识符。 (继承自 Control) |
| ValidateRequestMode |
获取或设置指示控件是否检查来自浏览器的客户端输入是否具有潜在危险值的值。 (继承自 Control) |
| ViewState |
获取状态信息的字典,这些信息使您可以在同一页的多个请求间保存和还原服务器控件的视图状态。 (继承自 Control) |
| ViewStateIgnoresCase |
获取一个值,该值指示 StateBag 对象是否不区分大小写。 (继承自 Control) |
| ViewStateMode |
获取或设置此控件的视图状态模式。 (继承自 Control) |
| Visible |
获取或设置一个值,该值指示服务器控件是否作为 UI 呈现在页上。 (继承自 Control) |
方法
事件
| AbortTransaction |
当用户结束交易时发生。 (继承自 TemplateControl) |
| CommitTransaction |
当事务完成时发生。 (继承自 TemplateControl) |
| DataBinding |
当服务器控件绑定到数据源时发生。 (继承自 Control) |
| Disposed |
当从内存释放服务器控件时发生,这是请求 ASP.NET 页时服务器控件生存期的最后阶段。 (继承自 Control) |
| Error |
当引发未经处理的异常时发生。 (继承自 TemplateControl) |
| Init |
当服务器控件初始化时发生;初始化是控件生存期的第一步。 (继承自 Control) |
| Load |
当服务器控件加载到 Page 对象中时发生。 (继承自 Control) |
| PreRender |
在加载 Control 对象之后、呈现之前发生。 (继承自 Control) |
| Unload |
当服务器控件从内存中卸载时发生。 (继承自 Control) |
显式接口实现
扩展方法
| FindDataSourceControl(Control) |
返回与指定控件的数据控件关联的数据源。 |
| FindFieldTemplate(Control, String) |
返回指定控件的命名容器中指定列的字段模板。 |
| FindMetaTable(Control) |
返回包含数据控件的元表对象。 |
| GetDefaultValues(INamingContainer) |
为指定数据控件获取默认值的集合。 |
| GetMetaTable(INamingContainer) |
为指定数据控件获取表元数据。 |
| SetMetaTable(INamingContainer, MetaTable) |
为指定数据控件设置表元数据。 |
| SetMetaTable(INamingContainer, MetaTable, IDictionary<String,Object>) |
为指定数据控件设置表元数据和默认值映射。 |
| SetMetaTable(INamingContainer, MetaTable, Object) |
为指定数据控件设置表元数据和默认值映射。 |
| TryGetMetaTable(INamingContainer, MetaTable) |
确定表元数据是否可用。 |
| EnableDynamicData(INamingContainer, Type) |
为指定数据控件启用动态数据行为。 |
| EnableDynamicData(INamingContainer, Type, IDictionary<String,Object>) |
为指定数据控件启用动态数据行为。 |
| EnableDynamicData(INamingContainer, Type, Object) |
为指定数据控件启用动态数据行为。 |