Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why we use @Master type?

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)

like image 843
Usman Avatar asked Jan 20 '12 19:01

Usman


People also ask

What is master page why it is needed?

Advantages of master pages include the following: They allow you to centralize the common functionality of your pages so that you can make updates in just one place. They make it easy to create one set of controls and code and apply the results to a set of pages.

What is master page explain with example?

A master page provides a template for other pages, with shared layout and functionality. The master page defines placeholders for the content, which can be overridden by content pages. The output result is a combination of the master page and the content page. The content pages contain the content you want to display.

What is master type directive?

To access members of a specific master page from a content page, you can create a strongly typed reference to the master page by creating a @ MasterType directive. The directive allows you to point to a specific master page.

What is master page and content page?

The master page establishes a layout and includes one or more ContentPlaceHolder controls for replaceable text and controls. The content page includes only the text and controls that are merged at run time with the master page's ContentPlaceHolder controls.


2 Answers

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;
like image 114
Lloyd Avatar answered Sep 28 '22 11:09

Lloyd


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.

like image 41
Ryan Prechel Avatar answered Sep 28 '22 12:09

Ryan Prechel