Maybe you have encountered the MasterType and Reference directives in ASP.Net 2.0 and wonder what they are used for?
When you create a MasterPage you probably want to add some methods or properties to the MasterPage, for example, exposing a control located on the MastePage so it could be accessible from your content page, or calling a method that will perform something.
By getting a method or property from a MasterPage from your content page, you can cast the Master class to the type of your MasterPage.
For example:
stirng value = ((main_master)Master).MyPropety
The main_master is the class name of your master page. Usually the name of the master page but the dot is replaced with an underscore.
With the MasterType directive your don't need to do the casting. MasterType will make your master page's strong-typed properties or method available through the Master class.
Here is an example where the MasterType is used:
Base Master Page
<%@ Master Language="C#" CompileWith="MasterPage.master.cs" ClassName="MasterPage_master" %>
<html>
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:contentplaceholder id="ContentPlaceHolder1" runat="server">
</asp:contentplaceholder>
</div>
</form>
</body>
</html>
Base MasterPage code
public partial class MasterPage_master
{
public string Name
{
get { return "The Base Master Page"; }
}
}
Content page
<%@ Page Language="C#" MasterPageFile="~/MasterPage.master" CompileWith="Default.aspx.cs" ClassName="Default_aspx" Title="Untitled Page" %>
<%@ MasterType virtualPath="MasterPage.master" %>
Content page code
public partial class Default_aspx
{
void Page_Load()
{
Response.Write(Master.Name);
}
}
The example above will get the value from the Name property that is added to the MasterPage, and display it when the Content page is requested.
If you use nested master pages and want to get access to a property or method added to the base master page, you can use the Reference directive to make the content page aware of the base master page members.
Here is an example where a content page gets the value of the base master page's Name property:
Base Master Page
<%@ Master Language="C#" CompileWith="MasterPage.master.cs" ClassName="MasterPage_master" %>
<html>
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:contentplaceholder id="ContentPlaceHolder1" runat="server">
</asp:contentplaceholder>
</div>
</form>
</body>
</html>
Base Master page code
public partial class MasterPage_master
{
public string Name
{
get { return "The Base Master Page"; }
}
}
Sub Master Page
<%@ Master masterPageFile="MasterPage.master" Language="C#" CompileWith="MasterPage2.master.cs" ClassName="MasterPage2_master" %>
<%@ MasterType virtualPath="MasterPage.master" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="server">
<asp:contentplaceholder id="ContentPlaceHolder10" runat="server">
</asp:contentplaceholder>
</asp:Content>
Content page
<%@ Page Language="C#" MasterPageFile="~/MasterPage2.master" CompileWith="Default2.aspx.cs" ClassName="Default2_aspx" Title="Untitled Page" %>
<%@ MasterType virtualPath="MasterPage2.master" %>
<%@ Reference virtualPath="MasterPage.master" %>
Content page code
public partial class Default2_aspx
{
void Page_Load()
{
Response.Write(Master.Master.Name);
}
}
To get the Name property of the base Master page you have to first use the content page Master class and then call its Master class:
Master.Master.Name