Home
    Shop
    Advertise
    Write For Us
    Affiliate
    Newsletter
    Contact

Interaction Between Master Page and Content Page

Master page and content page work together to produce output to client. Sometimes, interaction between master page and content page is needed. Although too much of interaction between master page and content page can cause maintenance problems in future, it could be useful in some scenarios. There are few ways how content pages can access elements of master page.

 

Create public property on master page

Easy way to manipulate elements of master page with code on content page is to create public property in master page. Code on master page could look like this:

[ C# ]

using System;
using System.Web.UI;
using System.Web.UI.WebControls;
 
public partial class MasterPage : System.Web.UI.MasterPage
{
 // Public property that will be used to manipulate control on
 // Master page
 public string PropertyOnMasterPage
 {
   get
   {
     // Get value of control on master page
     return Label1.Text;
   }
   set
   {
     // Set new value for control on master page
     Label1.Text = value;
   }
 }
}

[ VB.NET ]

Imports System
Imports System.Web.UI
Imports System.Web.UI.WebControls
 
Partial Public Class MasterPage
 Inherits System.Web.UI.MasterPage
 
 ' Public property that will be used to manipulate control on
 ' Master page
 Public Property PropertyOnMasterPage() As String
   Get
     ' Get value of control on master page
     Return Label1.Text
   End Get
   Set(ByVal value As String)
     ' Set new value for control on master page
     Label1.Text = value
   End Set
 End Property
End Class

Also, you need MasterType directive added in content page:

<%@ Page Title="" Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
 
<%@ MasterType VirtualPath="~/MasterPage.master" %>
 
<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
</asp:Content>

MasterType directive has VirtualPath attribute. Value of this property should be the same as value of MasterPageFile attribute of Page directive.

Now, you can use this property in code on content page. IntelliSense will recognize new property in code view, like on image bellow:

Master page public property

Create public method on master page

Using the same logic, you can create public method on master page and call it from content page. Code in master page could look like this:

[ C# ]

// Public method located in master page, and will be
// called by content page
public string ShowCategory(int CategoryID)
{
 // public method code here
 
}

[ VB.NET ]

' Public method located in master page, and will be
' called by content page
Public Function ShowCategory(ByVal CategoryID As String) As String
 ' public method code here
 
End Function

Now, you can call this method from content page. Just remember to add MasterType directive to content page markup code, like we did it for public property.

Use FindControl() method in content page

Although using of public property or public method is common way to implement interaction between master page and content page, you can also use FindControl() method. For example, let say there is a Label control named Label1 on master page. To access this control from content page code, use snippet like this:

[ C# ]

protected void Page_Load(object sender, EventArgs e)
{
 // Get reference to control located on master page
 Label lb = (Label)Page.Master.FindControl("Label1");
 // Manipulate properties of control on master page
 // with code on content page
 lb.Text = "Hello from content page";
 lb.ForeColor = System.Drawing.Color.Red;
 lb.Font.Bold = true;
}

[ VB.NET ]

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
 ' Get reference to control located on master page
 Dim lb As Label = Page.Master.FindControl("Label1")
 ' Manipulate properties of control on master page
 ' with code on content page
 lb.Text = "Hello"
 lb.ForeColor = System.Drawing.Color.Red
 lb.Font.Bold = True
End Sub

FindControl() method works in both direction. It can be used to manipulate control located in content page with code on master page. Only difference is that we will not use Page.Master.FindControl() method, but Page.FindControl(), with code like this somewhere in master page:

[ C# ]

// Get reference to control located on content page
Label lb = (Label)Page.FindControl("Label1");
// As in previous example, now you can
// manipulate properties, methods or events of control on content page

// with code on master page
...

[ VB.NET ]

' Get reference to control located on content page
Dim lb As Label = Page.FindControl("Label1")
' As in previous example, now you can
' manipulate properties, methods or events of control on content page
' with code on master page
...

Note that on this way you can load your custom user controls too, change its properties or execute methods and events.

Conclusion

As you see, it is pretty easy to communicate between master page and content page. However, just because you can, doesn't mean you should use it. There is maintenance problem if you have a lot of interaction between content and master page. During the time, you'll probably add new master pages to site. With every change in interaction you must change code in all master pages. The solution is to avoid interaction if possible, or inherit all master pages from one base master page that contains all needed public members. On this way, all changes in code will affect only parent master page. Often, you can just add new ContentPlaceHolder in master page and avoid interaction completely.


Tutorial toolbar:  Tell A Friend  |  Add to favorites  |  Feedback  |