为什么我们使用@Master类型?

发布于 2024-12-28 06:36:37 字数 333 浏览 3 评论 0原文

如果我们有一个母版页和一个内容页。那么内容页@Page指令看起来像这样

<%@ Page Language="C#" MasterPageFile="~/Site1.Master" .... />

,为了访问内容页中的母版页控件,我们应该使用

<%@ MasterType VirtualPath="~/Site1.Master" %>

so,我的问题是这为什么我们使用@MasterType指令当我们已经在 @page 指令中定义该内容页面位于母版页中时(此处为 Site1.Master)

if we have a master page and a content page.so the content page @Page directive look like as

<%@ Page Language="C#" MasterPageFile="~/Site1.Master" .... />

so , in order to access master page controls in content page we should have to use

<%@ MasterType VirtualPath="~/Site1.Master" %>

so , my question is this why we use @MasterType directive when we already define in the @page directive that this content page is in the master page (here -- Site1.Master)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦在深巷 2025-01-04 06:36:37

来自 Microsoft Docs,您是定义 Master 属性的类型,它允许您访问 MasterPage 派生类的属性。

提供一种在从 Master 属性访问母版页时创建对 ASP.NET 母版页的强类型引用的方法。

举个例子:

this.Master.SomePublicPropertyOfMaster = Value;

From Microsoft Docs you are defining the type of the Master property, which allows you to access the properties of your MasterPage derived class.

Provides a way to create a strongly typed reference to the ASP.NET master page when the master page is accessed from the Master property.

As an example:

this.Master.SomePublicPropertyOfMaster = Value;
小矜持 2025-01-04 06:36:37

使用 MyMasterPage 类型指定 @MasterType 指令会在代码隐藏类中产生以下属性定义:

public new MyMasterPage Master {
  get {
    return ({MyMasterPage})base.Master;
  }
}

此属性定义由 BuildMiscClassMembers 方法TemplateControlCodeDomTreeGenerator 类的。

Specifying the @ MasterType directive with a type of MyMasterPage results in the following property definition in the code behind class:

public new MyMasterPage Master {
  get {
    return ({MyMasterPage})base.Master;
  }
}

This property definition is created by the BuildMiscClassMembers method of the TemplateControlCodeDomTreeGenerator class.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文