Modifying Master Page Content


Master Pages enable you to display the same content in multiple content pages. You'll quickly discover that you need to override the content displayed by a Master Page in the case of particular content pages.

For example, normally the Master Page contains the opening and closing HTML tags, including the <title> tag. This means that every content page will display the same title. Normally, you want each page to display a unique title.

In this section, you learn multiple techniques of modifying Master Page content from a content page.

Using the Title Attribute

If you only need to modify the title displayed in each content page, then you can take advantage of the <%@ Page %> directive's Title attribute. This attribute accepts any string value.

For example, the page in Listing 5.13 includes a Title attribute, which sets the title of the current content page to the value Content Page Title.

Listing 5.13. TitleContent.aspx

<%@ Page Language="VB" MasterPageFile="~/SimpleMaster.master"     Title="Content Page Title" %> <asp:Content          ContentPlaceHolder     Runat="Server">     Content in the first column     <br />Content in the first column     <br />Content in the first column     <br />Content in the first column     <br />Content in the first column </asp:Content> <asp:Content          ContentPlaceHolder     Runat="Server">     Content in the second column     <br />Content in the second column     <br />Content in the second column     <br />Content in the second column     <br />Content in the second column </asp:Content> 

There is one requirement for the Title attribute to work. The HTML <head> tag in the Master Page must be a server-side Head tag. In other words, the <head> tag must include the runat="server" attribute. When you create a new Web Form or Master Page in Visual Web Developer, a server-side <head> tag is automatically created.

Using the Page Header Property

If you need to programmatically change the Title or Cascading Style Sheet rules included in a Master Page, then you can use the Page.Header property. This property returns an object that implements the IPageHeader interface. This interface has the following two properties:

  • StyleSheet

  • Title

For example, the content page in Listing 5.14 uses the SimpleMaster.master Master Page. It changes the Title and background color of the Master Page.

Listing 5.14. HeaderContent.aspx

<%@ Page Language="VB" MasterPageFile="~/SimpleMaster.master" %> <script runat="server">     Private Sub Page_Load()         ' Change the title         Page.Header.Title = String.Format("Header Content ({0})", DateTime.Now)         ' Change the background color         Dim myStyle As New Style()         myStyle.BackColor = System.Drawing.Color.Red         Page.Header.StyleSheet.CreateStyleRule(myStyle, Nothing, "html")     End Sub </script> <asp:Content          ContentPlaceHolder     Runat="Server">     Content in the first column     <br />Content in the first column     <br />Content in the first column     <br />Content in the first column     <br />Content in the first column </asp:Content> <asp:Content          ContentPlaceHolder     Runat="Server">     Content in the second column     <br />Content in the second column     <br />Content in the second column     <br />Content in the second column     <br />Content in the second column </asp:Content> 

The Page.Header property returns the server-side<head> tag contained in the Master Page. You can cast the object returned by this property to an HTMLHead control. For example, the page in Listing 5.15 modifies the Master Page <meta> tags (the tags used by search engines when indexing a page).

Listing 5.15. MetaContent.aspx

<%@ Page Language="VB" MasterPageFile="~/SimpleMaster.master" %> <script runat="server">     Private Sub Page_Load()         ' Create Meta Description         Dim metaDesc As New HtmlMeta()         metaDesc.Name = "DESCRIPTION"         metaDesc.Content = "A sample of using HtmlMeta controls"         ' Create Meta Keywords         Dim metaKeywords As New HtmlMeta()         metaKeywords.Name = "KEYWORDS"         metaKeywords.Content = "HtmlMeta,Page.Header,ASP.NET"         ' Add Meta controls to HtmlHead         Dim head As HtmlHead = CType(Page.Header, HtmlHead)         head.Controls.Add(metaDesc)         head.Controls.Add(metaKeywords)     End Sub </script> <asp:Content          ContentPlaceHolder     Runat="Server">     Content in the first column     <br />Content in the first column     <br />Content in the first column     <br />Content in the first column     <br />Content in the first column </asp:Content> <asp:Content          ContentPlaceHolder     Runat="Server">     Content in the second column     <br />Content in the second column     <br />Content in the second column     <br />Content in the second column     <br />Content in the second column </asp:Content> 

Notice that the Page_Load() method in Listing 5.15 creates two HtmlMeta controls. The first control represents a Meta Description tag and the second control represents a Meta Keywords tag. Both HtmlMeta controls are added to the HtmlHead control's Controls collection.

When the page is rendered, the following tags are added to the <head> tag:

<meta name="DESCRIPTION" content="A sample of using HtmlMeta controls" /> <meta name="KEYWORDS" content="HtmlMeta,Page.Header,ASP.NET" /> 


Warning

You receive a NullReference exception if you use the Page.Header property when the Master Page does not contain a server-side <head> tag.


Exposing Master Page Properties

You can expose properties and methods from a Master Page and modify the properties and methods from a particular content page. For example, the Master Page in Listing 5.16 includes a public property named BodyTitle.

Listing 5.16. PropertyMaster.master

<%@ Master Language="VB" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> <script runat="server">     Public Property BodyTitle() As String         Get             Return ltlBodyTitle.Text         End Get         Set(ByVal Value As String)             ltlBodyTitle.Text = value         End Set     End Property </script> <html xmlns="http://www.w3.org/1999/xhtml" > <head  runat="server">     <style type="text/css">         html         {             background-color:silver;         }         .content         {             margin:auto;             width:700px;             background-color:white;             padding:10px;         }         h1         {             border-bottom:solid 1px blue;         }     </style>     <title>Property Master</title> </head> <body>     <form  runat="server">     <div >     <h1><asp:Literal  runat="server" /></h1>     <asp:contentplaceholder                  runat="server" />     </div>     </form> </body> </html> 

The BodyTitle property enables you to assign a title that is rendered in a header tag in the body of the page (see Figure 5.6).

Figure 5.6. Displaying a body title.


Because the BodyTitle property is exposed as a public property, you can modify it from a particular content page. The page in Listing 5.17 assigns the value "The Body Title" to the BodyTitle property.

Listing 5.17. PropertyContent.aspx

<%@ Page Language="VB" MasterPageFile="~/PropertyMaster.master" %> <%@ MasterType VirtualPath="~/PropertyMaster.master" %> <script runat="server">     Private Sub Page_Load()         If Not Page.IsPostBack Then             Master.BodyTitle = "The Body Title"         End If     End Sub </script> <asp:Content          ContentPlaceHolder     Runat="Server">     Content, Content, Content, Content     <br />Content, Content, Content, Content     <br />Content, Content, Content, Content     <br />Content, Content, Content, Content     <br />Content, Content, Content, Content </asp:Content> 

You should notice several things about the page in Listing 5.17. First, notice that you can refer to the Master Page by using the Master property. In the Page_Load() method in Listing 5.17, the BodyTitle property of the Master Page is assigned a value with the following line of code:

Master.BodyTitle = "The Body Title"; 


You should also notice that the page in Listing 5.17 includes a <%@ MasterType %> directive. This directive automatically casts the value of the Master property to the type of the Master Page. In other words, it casts the Master Page to the PropertyMaster type instead of the generic MasterPage type.

If you want to be able to refer to a custom property in a Master Page, such as the BodyTitle property, then the value of the Master property must be cast to the right type. The BodyTitle property is not a property of the generic MasterPage class, but it is a property of the PropertyMaster class.

Using FindControl with Master Pages

In the previous section, you learned how to modify a property of a control located in a Master Page from a content page by exposing a property from the Master Page. You have an alternative here. If you need to modify a control in a Master Page, you can use the FindControl() method in a content page.

For example, the Master Page in Listing 5.18 includes a Literal control named BodyTitle. This Master Page does not include any custom properties.

Listing 5.18. FindMaster.master

<%@ Master Language="VB" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head  runat="server">     <style type="text/css">         html         {             background-color:silver;         }         .content         {             margin:auto;             width:700px;             background-color:white;             padding:10px;         }         h1         {             border-bottom:solid 1px blue;         }     </style>     <title>Find Master</title> </head> <body>     <form  runat="server">     <div >     <h1><asp:Literal  runat="server" /></h1>     <asp:contentplaceholder                  runat="server" />     </div>     </form> </body> </html> 

The content page in Listing 5.19 modifies the Text property of the Literal control located in the Master Page. The content page uses the FindControl() method to retrieve the Literal control from the Master Page.

Listing 5.19. FindContent.aspx

<%@ Page Language="VB" MasterPageFile="~/FindMaster.master" %> <script runat="server">     Private Sub Page_Load()         If Not Page.IsPostBack Then             Dim ltlBodyTitle As Literal = CType(Master.FindControl("ltlBodyTitle"), Literal)             ltlBodyTitle.Text = "The Body Title"         End If     End Sub </script> <asp:Content          ContentPlaceHolder     Runat="Server">     Content, Content, Content, Content     <br />Content, Content, Content, Content     <br />Content, Content, Content, Content     <br />Content, Content, Content, Content     <br />Content, Content, Content, Content </asp:Content> 

The FindControl() method enables you to search a naming container for a control with a particular ID. The method returns a reference to the control.




ASP. NET 2.0 Unleashed
ASP.NET 2.0 Unleashed
ISBN: 0672328232
EAN: 2147483647
Year: 2006
Pages: 276

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net