Master
Pages A Master
Page Example
Master Pages
ASP.NET 2.0 introduces a new concept known as master pages, in
which a common base master file contains the common look and feel
and standard behavior for all the pages in your application. Once
the common content is placed in the master page, the content pages
(child pages) can inherit content from the master pages apart from
adding their content to the final output. To allow the content page
to add its own content, you add placeholders (known as
ContentPlaceHolder control) in the master page that will be utilized
by the content pages to add their custom content. When users request
the content pages, the output of the content pages are merged with
the output of the master page, resulting in an output that combines
the layout of the master page with the output of the content
page.
Master pages are saved with the file extension .master. Apart
from containing all the contents that are required for defining the
standard look and feel of the application, the master pages also
contain all the top-level HTML elements for a page, such as
<html>, <head>, and <form>. As mentioned
previously, the master pages also contain one or more content
placeholders that are used to define regions that will be rendered
through the content pages.
Now that you have had a general understanding of master pages,
take a look at an example. First, create a master page named
CommonPage.master and add the code shown in Listing 1-1.
Listing 1-1: A Master Page Example <%@
master language=”C#”
%> <html> <head
runat=”server”>
<title>Master
Page</title>
</head>
<body>
<form
runat=”server”>
Master Page
Content
<br/>
<b>
<asp:ContentPlaceHolder
id=”MiddleContent”
runat=”server”> </asp:ContentPlaceHolder>
</b>
</form> </body> </html>
Apart from looking at the file extension, you can also identify a
master file by looking at the new page directive named master at the
top of the page. This declarative is used to identify that the
current page is a master page and prevents users from requesting the
page from a browser. Inside the code, the code contains an element
named asp:ContentPlaceHolder, which will be used by all the content
pages to render appropriate content that is specific to their pages.
That’s all there is to creating the master page. To create a content
page, add a new ASP.NET page named ContentPage.aspx and modify the
code as follows:
<%@ page language=”c#” MasterPageFile=”~/CommonPage.master”
%> <asp:Content id=”Content1”
ContentPlaceHolderID=”MiddleContent”
runat=”server”> Child Page Content </asp:Content>
The code required for the content page is very simple and
straightforward. As part of the page directive, specify a new
attribute named MasterPageFile that is used to identify the name of
the master page that you want to utilize. This example uses the
master page created in Listing 1-1. Next, you have a new element
named asp:Content that is used to associate the
asp:ContentPlaceHolder element in the master page with the content
page. This is done through the use of the ContentPlaceHolderID
attribute. That’s all that is there to creating a master page and
using the master page from a content page. Now, if you request the
content page from a browser, you will get the output that is
produced by merging the master page with the content page.
More Related links
A Master
page offers a template for one or more web forms. It defines
placeholders for the content, which can be overridden by the content
pages. The content pages contains only content........
Article on ASP.NET Server Control Events.
This articles explains difference between Server Control and HTML
control in ASP.NET.
This articles describes the navigation ways
available in ASP.NET.
This includes introduction of .Net framework, .Net framework
architecture, role of assembly and GAC.
ASP.NET can also impersonate a specific account you specify in
web.config.........
The passing of the control from the child to the parent is called
as bubbling..........
ASP.NET runs inside the process of IIS due to which there are two
authentication layers which exist in the
system.............
|