Master Page in ASP.NET


  1. The extension of MasterPage is '.master'.
  2. MasterPage cannot be directly accessed from the client because it just acts as a template for the other Content Pages.
  3. In a MasterPage we can have content either inside ContentPlaceHolder or outside it. Only content inside the ContentPlaceHolder can be customized in the Content Page.
  4. We can have multiple masters in one web application.
  5. A MasterPage can have another MasterPage as Master to it.
  6. The content page content can be placed only inside the content tag.
  7. Controls of MasterPage can be programmed in the MasterPage and content page but a content page control will never be programmed in MasterPage.
  8. A master page of one web application cannot be used in another web application.
  9. The MasterPageFile property of a webform can be set dynamically and it should be done either in or before the Page_PreInit event of the WebForm. Page.MasterPageFile = "MasterPage.master". The dynamically set Master Page must have the ContentPlaceHolder whose content has been customized in the WebForm.
  10. The order in which events are raised: Load (Page) a Load (Master) a LoadComplete (Page) i.e. if we want to overwrite something already done in Load event handler of Master then it should be coded in the LoadComplete event of the page.
  11. Page_Load is the name of method for event handler for Load event of Master. (it's not Master_Load).


Adding a MasterPage to the Project

  1. Add a new MasterPage file (MainMaster.master) to the Web Application.
  2. Change the Id of ContentPlaceHolder in <Head> to "cphHead" and the Id "ContentPlaceHolder1" to "cphFirst"
  3. Add one more ContentPlaceHolder (cphSecond) to Master page.
  4. To the master page add some header, footer and some default content for both the content place holders.
    1. <form id="form1" runat="server">  
    2. Header...<br />  
    3. <asp:ContentPlaceHolder id="cphFirst" runat="server">  
    4.                 This is First Content Place Holder (Default)  
    5. </asp: ContentPlaceHolder>  
    6. <br />  
    7. <asp:ContentPlaceHolder ID="cphSecond" runat="server">  
    8. This is Second Content Place Holder (Default)  
    9. </asp:ContentPlaceHolder>  
    10. <br /> Footer...  
    11. </form>
  5. To the web application add a WebForm (Default.aspx) a Check (Select Master Page) in New Item Dialog
  6. Note the attribute "MasterPageFile" in @Page directive of the WebForm.
  7. Delete the <content tag for the ContentPlaceHolderId="cphSecond".
  8. Run the WebForm - The output rendered includes the Header, Footer, Contentof cphSecond in Master and the content of <content tag for ContentPlaceHolderId="cphFirst" in webform.
  9. Here we understood the importance of ContentPlaceHolder in Master and Content in WebForm.
  10. Add a Label in the master page (outside ContentPlaceHolder)
    1. <asp:Label ID="lblMaster" runat="server" Text="In Master"/>
  11. Add a Button to WebForm (inside content tag)
    1. <asp:Button ID="btnDemo" runat="server" onclick="btnDemo_Click" Text="Set Label of Master" />
  12. Handle the Click event of above button and add to it the code as mentioned below.
    1. protected void btnDemo_Click(object sender, EventArgs e)  
    2. {  
    3.       //Get reference to Label control (lblMaster) in the master page.  
    4.       Label lbl = (Label)Master.FindControl("lblMaster");  
    5.       lbl.Text = "From WebForm Page...";  
    6. }
  13. Run the WebForm and Click on Button to see that the text in master page Label has changed.
  14. To the class in MainMaster.master.cs add the following property. 
    1. public Label MasterLabel  
    2. {  
    3.        get { return lblMaster; }  
    4. }
  15. To the Default.aspx add the following
    1. <%@ MasterType  VirtualPath="~/MainMaster.master" %>
  16. Replace the existing code in btnDemo_Click with the following.
    1. //To set Text of Label in master page using the public property MasterLabel  
    2. Master.MasterLabel.Text = "From WebForm";  
    3.  //The above line would work only if <%@MasterType Directive is added to current page  

Importance of UniqueID and ClientId 

  1. Add a TextBox and HTML Button to webform / content page which has master page.
    1. <asp:TextBox runat="server" ID="txtWebForm"/>  
    2. <input type="button" value="ClientButton" onclick="ShowAlert()" />    
  2. Add the following to the webform (cphHead is Id of ContentPlaceHolder added to Head section of Master Page)
    1. <asp:Content ID="cntHead" ContentPlaceHolderID="cphHead" runat="server">  
    2.     <script language="javascript" type="text/javascript">  
    3.         function ShowAlert()  
    4.         {  
    5.             txt = document.<%=Page.Form.Name%>.<%=txtWebForm.UniqueID%>  
    6.             alert(txt.value);  
    7.            }  
    9.     </script>  
    10. </asp:Content>


Similar Articles