Master Page in ASP.Net
ASP.NET master pages allow you to
create a consistent layout for the pages in your application. A single master
page defines the look and feel and standard behavior that you want for all of
the pages (or a group of pages) in your application. You can then create
individual content pages that contain the content you want to display. When
users request the content pages, they merge with the master page to produce
output that combines the layout of the master page with the content from the
content page.
Creating Master Page
Creating Master Page
- Visual Studio 2005.
- Create new ASP.NET Web site, enter name as MasterPagesApp.
- Select Location as File System, and enter the path as D:\WhidbeyWebSites\MasterPagesApp( or any other path ).
- Select the Language you are more comfortable with, for this we will use C#.
- After creating the new Web Site, you will find a page created for you named Default.aspx.
- Delete this page and right click the application from solution explorer. Select Add New Item, select Master Page from installed Visual Studio templates and name it MainLayout, and check the Place code in separate file checkbox; this will create you a code-behind in the language you select, we work here using C#.
Master Pages
A master page is an ASP.NET file with the extension .master (for example, MySite.master) with a predefined layout that can include static text, HTML elements, and server controls. The master page is identified by a special @ Master directive that replaces the @ Page directive that is used for ordinary .aspx pages. The directive looks like the following.
< %@ Master Language="C#" %>
The @ Master directive can contain most of the same directives that a @ Control directive can contain. For example, the following master-page directive includes the name of a code-behind file, and assigns a class name to the master page.
< %@ Master Language="C#" CodeFile="MasterPage.master.cs" Inherits="MasterPage" %>
In addition to the @ Master directive, the master page also contains all of the top-level HTML elements for a page, such as html, head, and form. For example, on a master page you might use an HTML table for the layout, an img element for your company logo, static text for the copyright notice, and server controls to create standard navigation for your site. You can use any HTML and any ASP.NET elements as part of your master page.
Replaceable Content
Placeholders
In addition to static text and controls that will appear on all pages, the master page also includes one or more ContentPlaceHolder controls. These placeholder controls define regions where replaceable content will appear. In turn, the replaceable content is defined in content pages. After you have defined the ContentPlaceHolder controls, a master page might look like the following.
< !DOCTYPE html PUBLIC "-//W3C//DTD XHTML
1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
< html >
< head runat="server" >
< title> Master page title< /title>
< /head>
< body>
< form id="form1" runat="server">
< table>
< tr>
< td> < asp:contentplaceholder id="Main" runat="server" /> < /td>
< td> < asp:contentplaceholder id="Footer" runat="server" /> < /td>
< /tr>
< /table>
< /form>
< /body>
< /html>
Content Pages
< %@ Page Language="C#" MasterPageFile="~/MasterPages/Master1.master" Title="Content Page"%>
In the content page, you create the content by adding Content controls and mapping them to ContentPlaceHolder controls on the master page.
Advantages of Master Pages
Master pages provide functionality that developers have traditionally created by copying existing code, text, and control elements repeatedly; using framesets; using include files for common elements; using ASP.NET user controls; and so on. Advantages of master pages include the following:
- They allow you to centralize the common functionality of your pages so that you can make updates in just one place.
- They make it easy to create one set of controls and code and apply the results to a set of pages. For example, you can use controls on the master page to create a menu that applies to all pages.
- They give you fine-grained control over the layout of the final page by allowing you to control how the placeholder controls are rendered.
- They provide an object model that allows you to customize the master page from individual content pages.
Run-time Behavior of
Master Pages
At run time, master pages are handled in the following sequence:
At run time, master pages are handled in the following sequence:
- Users request a page by typing the URL of the content page.
- When the page is fetched, the @ Page directive is read. If the directive references a master page, the master page is read as well. If this is the first time the pages have been requested, both pages are compiled.
- The master page with the updated content is merged into the control tree of the content page.
- The content of individual Content controls is merged into the corresponding ContentPlaceHolder control in the master page.
- The resulting merged page is rendered to the browser.
No comments:
Post a Comment