Gil Fink on .Net

Fink about IT

News

Microsoft MVP

MCPD Enterprise Applications Developer

Gil Fink

My Linkedin profile

Locations of visitors to this page

Creative Commons License

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.
© Copyright 2010 Gil Fink

Blog Roll

Hebrew Articles

Index Pages

My OSS Projects

Back to Basics – Using MasterType Directive

Back to Basics – Using MasterType Directive

This post is Back to Basics – Using MasterType Directive
a result of
a question that
I got from one
of the developers
that I work with.
The question was
how to use master page properties from an ASP.NET page.

Setting the Environment

The first thing that you want to do is to expose the properties
as public in the master page. For example this is code behind
of a simple master page that exposes the IsPageEnabled property:

public partial class Site1 : MasterPage
{
    #region Properties
 
    public bool IsPageEnabled { get; set; }
 
    #endregion
 
    #region Page events
 
    protected void Page_Load(object sender, EventArgs e)
    {
    }
 
    #endregion
}

The MasterType Directive

When we want to use the property in a page which reference the
previous master page we need to put the inside of it the MasterType
directive:

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

This directive declare that the master page of a page is strongly type.
By that simple directive we gain access to the master exposed public
properties and methods through the Master property of the page.
For example this is how in the page I’ll call the IsPageEnabled property of
the master page:

public partial class WebForm5 : Page
{
    #region Page Events
 
    protected void Page_Load(object sender, EventArgs e)
    {
        Master.IsPageEnabled = true;
    }
 
    #endregion
}

The markup of the page I use:

<%@ Page Title="" Language="C#" MasterPageFile="~/Site1.Master" AutoEventWireup="true"
    CodeBehind="WebForm5.aspx.cs" Inherits="TestModalDialog.WebForm5" %>
 
<%@ MasterType VirtualPath="~/Site1.Master" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
</asp:Content>

Summary

Lets sum up, when we use master pages we can expose properties
and methods for the pages. We use the MasterType directive in the
page in order to create a strong type master page inside of it. By
doing that we can pass data from the page to its master.
Very easy and very useful.

DotNetKicks Image

Comments

rtpHarry said:

This is a useful technique and one that I use regularly. The thing that really annoys me is having to add in the <%@ MasterType VirtualPath="~/Site1.Master" %> line to every content page. I dont know why it isn't implied by the MasterPageFile attribute? It was one of the things I was hoping for in .net 4 but it doesn't look like its coming...

# February 25, 2010 1:09 PM

Gil Fink said:

@rtpHarry,

As you wrote, it's very annoying to use this directive in each and every page but this is how to imply this method. Currently there is no other way.

# February 26, 2010 10:06 AM