Online Book Reader

Home Category

Programming Microsoft ASP.NET 4 - Dino Esposito [176]

By Root 5512 0
/>

The markup consists of an H2 element plus a couple of placeholders. This means that as the author of the master page, you always want a title string followed by the real content. However, the title string (the Label control in the code) is static. How can you make it dynamically settable from content pages? Here’s where master page properties fit in.

Programming the Master Page


You can use code in content pages to reference properties, methods, and controls in the master page, with some restrictions. The rule for properties and methods is that you can reference them if they are declared as public members of the master page. This includes public page-scope variables, public properties, and public methods.

Exposing Master Properties


To give an identity to a control in the master, you simply set the runat attribute and give the control an ID. Can you then access the control from within a content page? Not directly. The only way to access the master page object model is through the Master property. Note, though, that the Master property of the Page class references the master page object for the content page. This means that only public properties and methods defined on the master page class are accessible.

The following code enhances the previous master page to make it expose the text of the label as a public property:

public partial class MainContentMaster : MasterPage

{

protected void Page_Load(object sender, EventArgs e)

{

}

public String MainContentTitle

{

get { return MainContentTitle_Label.Text; }

set { MainContentTitle_Label.Text = value; }

}

}

The control’s protection level makes it inaccessible from the outside world, but the public property MainContentTitle defined in the preceding code represents a public wrapper around the Label’s Text property. In the end, the master page has an extra public property through which programmers can set the page description.

Invoking Properties on the Master


The Master property is the only point of contact between the content page and its master. The bad news is that the Master property is defined to be of type MasterPage; as such, it doesn’t know anything about any property or method definition specific to the master you’re really working with. In other words, the following code wouldn’t compile because no MainContentTitle property is defined on the MasterPage class:

public partial class NestedDemo : System.Web.UI.Page

{

protected void Page_Load(object sender, EventArgs e)

{

Master.MainContentTitle = "Nested demo";

}

}

What’s the real type behind the Master property?

The Master property represents the master page object as compiled by the ASP.NET runtime engine. This class follows the same naming convention as regular pages—ASP.XXX_master, where XXX is the name of the master file. Developers can override the default class name by setting the ClassName attribute on the @Master directive. The attribute lets you assign a user-defined name to the master page class:

<%@ Master ... Classname="ContentMaster" %>

In light of this, you would need code like that shown here:

((ASP.ContentMaster)Master).MainContentTitle = "Nested demo";

However, there are a couple of simpler alternatives—one for .NET 4 applications and one that works regardless of the .NET Framework version you’re using. If you compile your code for the .NET Framework 4, you can take advantage of the dynamic keyword in C#.

public partial class NestedDemo : Page

{

protected void Page_Load(Object sender, EventArgs e)

{

dynamic master = this.Master;

master.MainContentTitle = "Nested demo";

}

}

The dynamic keyword tells the compiler to suspend any further processing on the expression and just assume the syntax is fine. The compiler understands that the variable master is of type dynamic, and that’s enough. For this type, then, the compiler actually emits some code that at run time will try to resolve the expression through the services of the Dynamic Language Runtime (DLR) component

Return Main Page Previous Page Next Page

®Online Book Reader