Some Interesting facts about MasterPages

We all know that MasterPages are generally used for uniform look and feel in Web Applications. In this blog post I want to show some interesting facts about MasterPages in a Q&A format. So lets get started.

  • Can a MasterPage and a ContentPage be developed in 2 different languages? i.e. Suppose can I have a master page developed in VB.NET and a content page in C#.NET?

    Ans: Yes this is perfectly possible. Because the @Master directive doesn’t override the attributes set at the @Page directive level.

  • How is the relation established between a Master Page and a content page at runtime?

    Ans : The link is established through the ContentPlaceHolderID. Say for example I have a MasterPage called Default.Master , there is a section for ContentPlaceHolders with ID=”PageContent”. Now say for example I create a .aspx page and set the MasterPage as Default.Master, the ContentPlaceholderID in the Content Page will be “PageContent”. The following diagram details the same.


    MasterPage Source

    Language=”C#” AutoEventWireup=”true” CodeFile=”MasterPage.master.cs” Inherits=”MasterPage”

    PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN””&gt;

    id=”Head1″ runat=”server”>

    <title>Hello, master pages</title>



    id=”form1″ runat=”server”>

    id=”PageBody” runat=”server”>

    <!– derived pages will define content for this placeholder –>





    Derived Page(whose master has been set above) Source:

    Title=”” Language=”C#” MasterPageFile=”~/MasterPage.master” AutoEventWireup=”true” CodeFile=”Default2.aspx.cs” Inherits=”Default2″

    ID=”Content1″ ContentPlaceHolderID=”PageBody” Runat=”Server”>



Here the link is established through the ContentPlaceHolderID =”PageBody”.


  • I want all the pages in my application derive from a single master page. How can we achieve this without specifying the MasterPage for each of these pages.

    You can set this in the configuration file as below.



master=MasterPage.master />



  • How to access a control in a MasterPage from a derived page.

    This can be achieved through a combination of the Master Property and the @MasterType directive. Say for example I have a label in my master and I want to set the title of that label from my derived page. So I will add the @MasterType directive in my content page as follows



Now you can access the control from the derived page as follows

    Master.TitleBoxText = “This is the Master Page value”;

This entry was posted in Interesting Stuff. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s