asp.net.ph

Skip Navigation LinksHome > ASP.NET Web Forms > ASP.NET Master Pages > Working with Master Pages Programmatically

Working with Master Pages Programmatically

ASP.NET Web Forms   ASP.NET Master Pages


You can perform a number of common tasks programmatically with master pages, including the following:

  • Accessing members that are defined on the master page, which can consist of public properties and methods or controls.
  • Attaching master pages to a content page dynamically.

Accessing Members on the Master Page

To provide access to members of the master page, the Page class exposes a Master property.

To access members of a specific master page from a content page, you can create a strongly typed reference to the master page by creating a @ MasterType directive. The directive allows you to point to a specific master page. When the page creates its Master property, the property is typed to the referenced master page.

For example, you might have a master page named MasterPage.master that is the class name MasterPage_master. You might create @ Page and @ MasterType directives that look like the following:

<%@ Page masterPageFile="~/MasterPage.master" %>
<%@ MasterType virtualPath="~/MasterPage.master" %>

When you use a @ MasterType directive, such as the one in the example, you can reference members on the master page as in the following example:

CompanyName.Text = Master.CompanyName;
  C# VB

The Master property of the page is already typed to MasterPage_master.

Getting the Values of Controls on the Master Page

At run time, the master page is merged with the content page, so the controls on the master page are accessible to content page code. ( If the master page contains controls in a ContentPlaceHolder control, those controls are not accessible if overridden by a Content control from the content page. ) The controls are not directly accessible as master-page members because they are protected. However, you can use the FindControl method to locate specific controls on the master page. If the control that you want to access is inside a ContentPlaceHolder control on the master page, you must first get a reference to the ContentPlaceHolder control, and then call its FindControl method to get a reference to the control.

The following example shows how you can get a reference to controls on the master page. One of the controls being referenced is in a ContentPlaceHolder control and the other is not.

// gets a reference to a textbox control inside a contentplaceholder
ContentPlaceHolder mpContentPlaceHolder;
TextBox mpTextBox;
mpContentPlaceHolder = ( ContentPlaceHolder ) Master.FindControl ( "ContentPlaceHolder1" );
if ( mpContentPlaceHolder != null ) {
   mpTextBox = ( TextBox ) mpContentPlaceHolder.FindControl ( "TextBox1" );
   if ( mpTextBox != null ) {
      mpTextBox.Text = "TextBox found!";
   }
}

// gets a reference to a Label control that is not in a // ContentPlaceHolder control
Label mpLabel = ( Label ) Master.FindControl ( "masterPageLabel" );
if ( mpLabel != null ) {
   Label1.Text = "Master page label = " + mpLabel.Text;
}
  C# VB

You can access the contents of the master page’s ContentPlaceHolder controls by using the FindControl method, as shown above. If the ContentPlaceHolder control has been merged with content from a Content control, the ContentPlaceHolder control will not contain its default content. Instead, it will contain the text and controls that are defined in the content page.

More ...
Back to top


© 2025 Reynald Nuñez and asp.net.ph. All rights reserved.

If you have any question, comment or suggestion
about this site, please send us a note