Você está na página 1de 15

MASTER PAGES

® a master page is page that provides a


framework within which the content from
other pages can be displayed.
® The pages that provide the content that’s
displayed in a master page are called
content pages.
® The content of each page is displayed in the
master page’s content placeholder.

2
® To add a master page to a project, choose
website ^ Add New Item command and
choose Master page. Specify the name of
the master page.
® The ContentPlaceHolder in the form element
of the page appears as a control in the web
forms designer.
® Any elements you add to the master page
outside of the content placeholder will
appear on every content page that uses the
master page.

3
® More than one content placeholder can be added
to the master page. In that case, each
placeholder displays a portion of the content of
each content page.
® The head element of a master page also contains
a content placeholder by default.
® An application can have more than one master
page and each content page specifies which
master page should be used to display the
content page
® The aspx file for a master page uses the
extension .master. The code behind file uses
.master.cs
<%@ Master Language="C#" AutoEventWireup="true"
CodeFile=MMasterPage.master.cs" Inherits="MasterPage" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<asp:ContentPlaceHolder id="head" runat="server">
</asp:ContentPlaceHolder>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ContentPlaceHolder id="ContentPlaceHolder1"
runat="server">
</asp:ContentPlaceHolder>
</div>
</form>
</body>
</html>

5
® Two ways to create a new content page:
■ Choose Website^Add New Item command, &
select web form, check the select master page
check box and click add. When the select a
Master page dialog box appears, select the
master page you want and click OK.
■ Select the master page in the solution explorer,
then choose the website^ Add content page
command.
® The aspx code for a new content page:
<%@ Page Title="" Language="C#"
MasterPageFile="~ /MasterPage.master"
AutoEventWi reup="true" CodeFi le="Default2. aspx.cs"
Inherits="Default2" %>
<asp:ContentID="Content1"
ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:ContentID="Content2"
ContentPlaceHolderID="ContentPlaceHolder1"
Runat="Server">
</asp:Content>
1. Add a MasterPageFile attribute to the page
directive that specifies the URL of the
master page.
2. Next, replace the div element that contains
the actual content of the page with the
content element.
3. Then if the head element contains other
elements used by the page, replace the
head element with a content element
4. Delete everything that is outside the
content elements except for the page
directive.
® In web.Config file
<system.web>

<page masterPageFile = “Site.Master” />


</system.web>
® In the Page Prelnit method
Protected void Page_PreInit(object sender,
EventArgs e)
{
MasterPageFile = “site.master”;
}

10
® The parent master page, is provides
elements for every page in the website.
Child Master pages can be used to provide
additional elements for different types of
pages.
® Add a child master page in the same way as
you add the master page & check the select
master page check box.
® When you use nested master pages, you can
create content pages for the parent master
page or any of the child master pages
® A content page can access a control in the
master page if you expose the control as a
public property in the master page.
® Example:
public Label MessageLabel
{
get ( return lblMessage)
set (lblMessage = value)
}
® The Master Type directive in an aspx file
specifies the name of the master page type.
e.g. <%@MasterType TypeName = “Site” %>
(site is the name of the master page)
® If the above directive is included in the
content page, the Master Property can be
used to access the exposed property of the
master page
e.g. this.Master.MessageLabel.Text = “there is one
item in your cart”;
this.Master.MessageLabel.Text = cart.Count + “
Items in your cart”;
® If the Master type directive is not used, the
Master property will return an object of type
master.
® We can then cast this object to an actual
type of the master page to access the
properties that are created.
■ MasterPage mp = (Site) this.Master;
■ mp.MessageLabel.Text = “There is one item in
your cart”;
■ Mp.MessageLabel.Text = “There are” + cart.count
+ “Items in your cart”;

Você também pode gostar