1. Trang chủ
  2. » Công Nghệ Thông Tin

ASP.NET 4 Unleased - p 29 pptx

10 270 0
Tài liệu đã được kiểm tra trùng lặp

Đang tải... (xem toàn văn)

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 10
Dung lượng 531,45 KB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

LISTING 5.12 FolderA\Web.Config The Master Page is applied only to content pages.. Modifying Master Page Content Master Pages enable you to display the same content in multiple co

Trang 1

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

Trang 2

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”>

Trang 3

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

Trang 4

<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

Trang 5

<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”

“http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd”>

<script runat=”server”>

public string BodyTitle

{

get { return ltlBodyTitle.Text; }

set { ltlBodyTitle.Text = value; }

}

Trang 6

</script>

<html xmlns=”http://www.w3.org/1999/xhtml” >

<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

Trang 7

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

Trang 8

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”

“http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd”>

<html xmlns=”http://www.w3.org/1999/xhtml” >

<head id=”Head1” runat=”server”>

<style type=”text/css”>

html

{

background-color:silver;

}

.content

{

margin:auto;

width:700px;

background-color:white;

padding:10px;

}

h1

{

Trang 9

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

Trang 10

<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;

Ngày đăng: 06/07/2014, 18:20

TỪ KHÓA LIÊN QUAN

TÀI LIỆU CÙNG NGƯỜI DÙNG

TÀI LIỆU LIÊN QUAN