Browse DevX
Sign up for e-mail newsletters from DevX


Introducing ASP.NET 2.0 Master Pages : Page 2

Building individual Web pages is easy. Building Web pages that adhere to specific site and application guidelines is much more difficult. Microsoft has come to the rescue with Master Pages, which builds page template capability directly into ASP.NET 2.0. This article shows you how to use the new Master Pages feature to create consistent page layouts for your Web applications.




Building the Right Environment to Support AI, Machine Learning and Deep Learning

Accessing Master Page Controls from Content Pages
Although Master Pages provide the common content for all the pages, sometimes you may need to access master page controls from within content pages and modify their values. To access the master page controls from a content page, invoke the Page.Master property, which returns a reference to the master page. Using that reference, you have direct access to the controls in the master page. This is a very powerful feature that gives content pages complete control over the content rendered through the master pages. There are two ways you can access the controls available in the master page.

  • In the first approach, you expose the controls in the master page using public properties. This means you need to create one property for each control that should be made available to the content pages.
  • In the second approach, you get a reference to the master page controls in a standard way—using the FindControl method—which is available because the Master class derives from System.Web.UI.Page class.
The following code shows an example that accesses a control in the master page from a content page.

<%@ master language="C#" %> <html> <head id="Head1" runat="server"> <title>Master Page</title> </head> <body> <form id="Form1" runat="server"> <table id="header" style="WIDTH: 100%; HEIGHT: 80px" cellspacing="1" cellpadding="1" border="1"> <tr> <td width="100%" style="TEXT- ALIGN: center"> <asp:label runat="server" id="Header"> This is the default header in the Master Page</asp:label> </td> </tr> </table> <b/> <table id="leftNav" style="WIDTH: 108px; HEIGHT: 100%" cellspacing="1" cellpadding="1" border="1"> <tr> <td style="WIDTH: 100px"> Left Navigation </td> </tr> </table> <table id="mainBody" style="LEFT: 120px; VERTICAL- ALIGN: top; WIDTH: 848px; POSITION: absolute; TOP: 94px; HEIGHT: 100%" border="1"> <tr> <td width="100%" style="VERTICAL-ALIGN: top"> <asp:contentplaceholder id="middleContent" runat="Server"> </asp:contentplaceholder> </td> </tr> </table> </form> </body> </html>

The preceding code is very similar to the code in Listing 1; however this version contains a server side control named "Header" in the header section. The following code shows how to access the Header control from a content page.

<%@ page language="C#" master="~/ExposeHeader.master" %> <script runat="server"> void Page_Load(object sender, System.EventArgs e) { Label headerLabel = (Label) Master.FindControl("Header"); headerLabel.Text = "This label content is set through the Page_Load event of the child page"; } </script> <asp:content id="Content1" contentplaceholderid="middleContent" runat="server"> This content is generated from the content page. </asp:content>

The preceding code starts by specifying the name of the master file to use. The Page_Load event invokes the FindControl method of the Master class, passing it the name of the master page control to find. The code casts the returned value to a Label control and then sets its Text value.

Apart from accessing all the controls of the master page, you can also access the public properties and methods exposed by the master page from the content pages using an early-bound approach. This early-bound approach not only increases the performance but also provides compile time type-checking, resulting in increased developer productivity.

Thanks for your registration, follow us on our social networks to keep up-to-date