18

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)

Erik Philips
  • 53,428
  • 11
  • 128
  • 150
Usman
  • 3,200
  • 3
  • 28
  • 47

2 Answers2

25

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;
Dai
  • 141,631
  • 28
  • 261
  • 374
Lloyd
  • 2,932
  • 2
  • 22
  • 18
  • @Lloyd..will you please tell me what do you mean by STRONGLY TYPE??since i am novice in programming so i have no idea about it... – Usman Jan 20 '12 at 19:35
  • 1
    It means the Type is explicitly declared, so you do not need to cast it ie: MyMasterPage masterPage = (MyMasterPage)this.MasterPage. – Lloyd Jan 20 '12 at 19:38
  • Link is dead. Try this one, https://msdn.microsoft.com/en-us/library/ms228274(v=vs.100).aspx – user3207158 Mar 07 '17 at 17:08
3

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.

Ryan Prechel
  • 6,592
  • 5
  • 23
  • 21