【问题标题】:why we use @Master type?为什么我们使用@Master 类型?
【发布时间】:2012-01-20 19:20:39
【问题描述】:

如果我们有一个母版页和一个内容页。所以内容页@Page 指令看起来像

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

所以,为了访问内容页面中的母版页控件,我们必须使用

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

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

【问题讨论】:

    标签: c# asp.net master-pages content-pages


    【解决方案1】:

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

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

    举个例子:

    this.Master.SomePublicPropertyOfMaster = Value;
    

    【讨论】:

    • @Lloyd..你能告诉我你所说的强类型是什么意思吗?因为我是编程新手,所以我不知道......
    • 表示Type是显式声明的,所以不需要强制转换即:MyMasterPage masterPage = (MyMasterPage)this.MasterPage.
    【解决方案2】:

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-17
      • 1970-01-01
      • 2021-10-25
      相关资源
      最近更新 更多