문제

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)

도움이 되었습니까?

해결책

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;

다른 팁

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top