advertisement
Login | Register   
  Include Code  Search Tips
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Download the code for this article
Partners & Affiliates
advertisement
advertisement
advertisement
advertisement
Average Rating: 1.8/5 | Rate this item | 854 users have rated this item.
Introducing ASP.NET 2.0 Master Pages (cont'd)
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.
advertisement


  • 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.

Previous Page: Introduction Next Page: Nesting Master Pages


Page 1: IntroductionPage 3: Nesting Master Pages
Page 2: Accessing Master Page Controls from Content Pages 
Please rate this item (5=best)
 1  2  3  4  5
advertisement