0% found this document useful (0 votes)
7 views24 pages

Master Pages

The document discusses the implementation of Master Pages in web development to create reusable and flexible user interface templates, addressing issues of code duplication and layout consistency. It outlines the structure of Master Pages, their benefits, and how to dynamically set and override them in content pages. Additionally, it provides best practices for using Master Pages effectively, including strong typing of controls and utilizing stylesheets.

Uploaded by

mridul.alex
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views24 pages

Master Pages

The document discusses the implementation of Master Pages in web development to create reusable and flexible user interface templates, addressing issues of code duplication and layout consistency. It outlines the structure of Master Pages, their benefits, and how to dynamically set and override them in content pages. Additionally, it provides best practices for using Master Pages effectively, including strong typing of controls and utilizing stylesheets.

Uploaded by

mridul.alex
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

Master Pages

Web Sites Today

Header
Navigation

Content

Footer
Master Pages
(Current Situation)

• Hundreds of pages difficult to update


• Layout is reused across site: header, footer, nav
• Code and markup not reused = duplication
• Current solutions:
• Include Files - unbalanced tags, many files
• User Controls - pages littered with controls, many files
• Tools-based solution – design-time only, commitment
Introducing Master Page

• Enable flexible and reusable user interface templates


• Contains
• Page layout (HTML Table)
• Content (Header, Footer, HTML,ASP.NET controls)
• Place holder regions for content in the pages
• Benefits
• Consistent Page Layout
• Shared UI & Code Elements
• Can be defined programmatically and declaratively
• Rich Visual Studio “Whidbey” designer-support
Design Time

MySite.master Default.aspx
<%@ Master %> <%@ Page Master=“~/mySiteMaster” %>
Header <asp:Content
Navigation ContentPlaceHolderId=MainContent
/>
<asp:ContentPlaceHolder
Id=MainContent />

Footer
Runtime

Header
Navigation

Default page content

Footer

 Default.aspx’s content replace the


contentPlace holder of the master page at
runtime
Master Pages
(Master Pages + Content Pages)
Master Page Programmability

• Page can access the master page


• Page.Master property
• Can access public properties in Master
• FindControl to access controls

• Page can set its master page dynamically


• Page.MasterFilePath property
• Dynamically set Master from the page
Points on Master Page

• The user can visually see the template in IDE when


creating the content pages.
• All the template items are shaded gray and not
editable.
• the only items are alterable are clearly shown in the
template. These workable areas, called content areas.
Originally defined in the master page itself.
• You can have more than one content area in master
page.
Rich UI
Choosing Master Page
Choosing Content Page
Specifying which Master Page to Use

• <%@ Page Language=“C#” MasterPageFile=“filename.master”


Title=“my title” %>
Specifying the master page in the web.config file

<configuration>
<system.web>
<page masterPageFile=“~mypage.master”/>
<system.web>
<configuration>
Overriding the Master Page

• <%@ Page Language=“C#”


MasterPageFile=“filename.master” Title=“my title” %>

• BySpecifying different master page in page


declarative
Applying to specific subset of pages

<configuration>
<location path=“AdministrativeArea”>
<system.web>
<page masterPageFile=“~mypage.master”/>
<system.web>
</location>
<configuration>
Working with page title

• By default the content page shares same title as master


page.
• You can avoid page’s title using Title attribute in the
@Page directive in the content pages.
• You can change title programmatically using Master
object
Coding page title

Protected void Page_LoadCompleted(object sender,


EventArgs e)
{
Master.Page.Title=“this page was generated on
DateTime.Now.ToString();
}
Getting Label’s Text value in content page

Protected void Page_LoadCompleted(object sender,


EventArgs e)
{
Label1.Text==(Master.FindControl(“Label1”) as
Label).Text;
}
A master page that exposes a custom property

string m_pageheading = "my company";


public string pageheading
{
get
{
return m_pageheading;
}
set
{
m_pageheading = value;
}
}
A content page that overrides property

Protected void Page_Load(object sender, EventArgs e)


{
Master.pageheading=“my company –division X”
}
Note: <%@ MasterType VirtualPath="" %>
Is required to access masterpage propeties
Exposing a server control from master page as a public property

public Label masterlabel


{
get
{
return Label1;
}
set
{
Label1 = value;
}
}
Overriding an attribute from label control

Protected void Page_Load(object sender, EventArgs e)


{
Master. masterlabel .Font.Size=25
}
(Best Practices)
Master Pages

• Do as much as you can inside the Style Sheet


• Strong Type Master Page Controls where you expect content pages
to access

<asp:Label ID="Title" RunAt="server" />

(CodeBehind)
public string TitleText
{ Master Page
get { return Title.Text; }
set { Title.Text = value; }
}

Master.TitleText = "Orders"; Content Page

You might also like