Tải bản đầy đủ (.pdf) (10 trang)

ASP.NET 4 Unleased - p 29 pptx

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (531.45 KB, 10 trang )

ptg
254
CHAPTER 5 Designing Websites with Master Pages
Registering Master Pages in Web Configuration
You can apply a Master Page to every content page in a particular folder or every content
page in an entire application. Rather than add a MasterPageFile attribute to individual
content pages, you can add a configuration option to the web configuration file.
For example, the web configuration file in Listing 5.12 applies the SimpleMaster.master
Master Page to every page contained in the same folder (or subfolder) as the web configu-
ration file.
LISTING 5.12 FolderA\Web.Config
<?xml version=”1.0”?>
<configuration>
<system.web>
<pages masterPageFile=”~/SimpleMaster.master” />
</system.web>
</configuration>
The Master Page is applied only to content pages. If a page does not contain any Content
controls—it is a normal ASP.NET page— the Master Page is ignored.
FIGURE 5.5 Displaying a Master Page relative image.
From the Library of Wow! eBook
ptg
255
Modifying Master Page Content
5
You can override the Master Page configured in the web configuration file in the case of a
particular content page. In other words, a MasterPageFile attribute in a content page
takes precedence over a Master Page specified in the web configuration file.
Modifying Master Page Content
Master Pages enable you to display the same content in multiple content pages. You
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 displays 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 need to modify only the title displayed in each content page, 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=”C#” MasterPageFile=”~/SimpleMaster.master”
Title=”Content Page Title” %>
<asp:Content
ID=”Content1”
ContentPlaceHolderID=”ContentPlaceHolder1”
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
ID=”Content2”
ContentPlaceHolderID=”ContentPlaceHolder2”
Runat=”Server”>
From the Library of Wow! eBook
ptg

256
CHAPTER 5 Designing Websites with Master Pages
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 CSS rules included in a Master Page,
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=”C#” MasterPageFile=”~/SimpleMaster.master” %>
<script runat=”server”>
void Page_Load()
{
// Change the title
Page.Header.Title = String.Format(“Header Content ({0})”, DateTime.Now);
// Change the background color
Style myStyle = new Style();
myStyle.BackColor = System.Drawing.Color.Red;

Page.Header.StyleSheet.CreateStyleRule(myStyle, null, “html”);
}
</script>
<asp:Content
ID=”Content1”
ContentPlaceHolderID=”ContentPlaceHolder1”
Runat=”Server”>
Content in the first column
<br />Content in the first column
From the Library of Wow! eBook
ptg
257
Modifying Master Page Content
5
<br />Content in the first column
<br />Content in the first column
<br />Content in the first column
</asp:Content>
<asp:Content
ID=”Content2”
ContentPlaceHolderID=”ContentPlaceHolder2”
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.

You can modify other header tags by using page properties. 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=”C#” MasterPageFile=”~/SimpleMaster.master” %>
<script runat=”server”>
void Page_Load()
{
// Create Meta Description
Page.MetaDescription = “A sample of using the ASP.NET 4.0 Meta Properties”;
// Create Meta Keywords
Page.MetaKeywords = “MetaDescription,MetaKeywords,ASP.NET”;
}
</script>
<asp:Content
ID=”Content1”
ContentPlaceHolderID=”ContentPlaceHolder1”
Runat=”Server”>
Content in the first column
From the Library of Wow! eBook
ptg
258
CHAPTER 5 Designing Websites with Master Pages
<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
ID=”Content2”

ContentPlaceHolderID=”ContentPlaceHolder2”
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_Load() method in Listing 5.15 uses two page-level properties. The first repre-
sents a Meta Description tag, and the second represents a Meta Keywords tag. 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” />
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=”C#” %>
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.1//EN”

<script runat=”server”>
public string BodyTitle
{
get { return ltlBodyTitle.Text; }
set { ltlBodyTitle.Text = value; }
}
From the Library of Wow! eBook
ptg
259

Modifying Master Page Content
5
</script>
<html xmlns=” >
<head id=”Head1” 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 id=”form1” runat=”server”>
<div class=”content”>
<h1><asp:Literal ID=”ltlBodyTitle” runat=”server” /></h1>
<asp:contentplaceholder
id=”ContentPlaceHolder1”

runat=”server” />
</div>
</form>
</body>
</html>
The BodyTitle property enables you to assign a title rendered in a header tag in the body
of the page (see Figure 5.6).
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.
From the Library of Wow! eBook
ptg
260
CHAPTER 5 Designing Websites with Master Pages
LISTING 5.17 PropertyContent.aspx
<%@ Page Language=”C#” MasterPageFile=”~/PropertyMaster.master” %>
<%@ MasterType VirtualPath=”~/PropertyMaster.master” %>
<script runat=”server”>
void Page_Load()
{
if (!Page.IsPostBack)
{
Master.BodyTitle = “The Body Title”;
}
}
</script>
<asp:Content
ID=”Content1”
ContentPlaceHolderID=”ContentPlaceHolder1”
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>
FIGURE 5.6 Displaying a body title.
From the Library of Wow! eBook
ptg
261
Modifying Master Page Content
5
You should notice several things about the page in Listing 5.17. First, 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”;
The page in Listing 5.17 includes a <%@ MasterType %> directive. This directive automati-
cally 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 refer to a custom property in a Master Page, such as the BodyTitle prop-
erty, 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=”C#” %>
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.1//EN”

<html xmlns=” >
<head id=”Head1” runat=”server”>
<style type=”text/css”>
html
{
background-color:silver;
}
.content
{
margin:auto;
width:700px;
background-color:white;
padding:10px;
}
h1
{
From the Library of Wow! eBook
ptg
262
CHAPTER 5 Designing Websites with Master Pages
border-bottom:solid 1px blue;
}
</style>
<title>Find Master</title>
</head>
<body>

<form id=”form1” runat=”server”>
<div class=”content”>
<h1><asp:Literal ID=”ltlBodyTitle” runat=”server” /></h1>
<asp:contentplaceholder
id=”ContentPlaceHolder1”
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=”C#” MasterPageFile=”~/FindMaster.master” %>
<script runat=”server”>
void Page_Load()
{
if (!Page.IsPostBack)
{
Literal ltlBodyTitle = (Literal)Master.FindControl(“ltlBodyTitle”);
ltlBodyTitle.Text = “The Body Title”;
}
}
</script>
<asp:Content
ID=”Content1”
ContentPlaceHolderID=”ContentPlaceHolder1”
Runat=”Server”>
Content, Content, Content, Content

<br />Content, Content, Content, Content
<br />Content, Content, Content, Content
From the Library of Wow! eBook
ptg
263
Loading Master Pages Dynamically
5
<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.
Loading Master Pages Dynamically
You can associate different Master Pages dynamically with a content page. This is useful in
two situations.
First, you can enable the users of your website to customize the appearance of the website
by loading different Master Pages. You can display a menu of Master Pages and allow your
users to pick their favorite layout.
Another situation in which loading Master Pages dynamically is useful concerns co-brand-
ing. Imagine that your company needs to make its website look like a partner website.
When users link to your website from the partner website, you don’t want users to know
that they are traveling to a new website. You can maintain this illusion by dynamically
loading different Master Pages based on a query string passed from a partner website.
A Master Page is merged with a content page early in the page execution life cycle. This
means that you cannot dynamically load a Master Page during the Page Load event. The
only event during which you can load a Master Page is during the Page PreInit event.
This is the first event raised during the page execution life cycle.
For example, the content page in Listing 5.20 dynamically loads one of two Master Pages
named Dynamic1.master and Dynamic2.master.
LISTING 5.20 DynamicContent.aspx

<%@ Page Language=”C#” MasterPageFile=”~/Dynamic1.master” %>
<script runat=”server”>
protected void Page_PreInit(object sender, EventArgs e)
{
if (Request[“master”] != null)
{
switch (Request[“master”])
{
case “Dynamic1”:
Profile.MasterPageFile = “Dynamic1.master”;
break;
From the Library of Wow! eBook

Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Tải bản đầy đủ ngay
×