How to manage Master Pages in Asp.net
This article tells you what are Master Pages and How to use the new Master Pages feature to create consistent page layouts for Web applications.
Introduction
Generally when we are developing a web Application, some web pages require consistent look like header and footer to achieve that we used user controls. And now Asp.net 2.0 has got a new feature called MASTER PAGES , one of the most anticipated feature provide a sanctioned and elegant way of designing pages in an application based on templates.Master Pages
Master Pages enables the developers to create a consistent look through out the application. Master page has .master as its extension. A master page contains a new ‘Master' directive. The implementation of Master Pages in ASP.NET 2.0 consists of two conceptual elements: Master Pages and Content Pages.
Master Pages act as the templates for Content Pages, and Content Pages provide content to populate pieces of Master Pages that require filling out.
The Master Page file serves as the template for other pages, so typically it will contain the top-level HTML elements, the main form, headers, footers, and such. Within the Master Page you add instances of the ContentPlaceHolder control at locations where you want Content Pages to supply page-specific content.
In contrast, Content Pages are just ordinary .aspx files that specify an associated Master Page in their page directive using the masterpagefile attribute. These pages must contain only instances of the Content control as their sole purpose is to supply content for the inherited Master Page template. Each Content control must map to a specific ContentPlaceHolder control defined in the referenced Master Page, the contents of which will be inserted into the Master Page's placeholder at rendering timeSmall Example of Master Page
Let us see how to create a simple master page
ExampleMaster.master
<%@ Master Language="C#" %>
<script language=c# runat=server>
// code
</script>
<html>
<head> <title>Master Pages in Aspx 2.0 </title>
<body>
<form runat=server>
<asp:ContentPlaceHolder ID="MyContentPlaceHolder" runat="server">
<h2>Hi this is Example for Master pages </h2>
</asp:ContentPlaceHolder>
</form>
</body>
</html>
In the above code there is a ContentPlaceHolder control there is some content. This content will render in any content page that doesn't reference this ContentPlaceHolder's ID, so in effect you can have default content on all your content pages which is overridden if you have content.
Master page can contain numerous content areas using ContentPlaceHolder controls just make sure to match up the ContentPlaceHolder's ID in your pages. Basically want happens is that when your .aspx page renders, it will cycle through all the Content controls and match up the PlaceHolder in the master page.Configuration Options of Master Pages
We can use a Master Page to be used by default for all pages in an application by adding a pages element to your web.config file specifying a common Master Page
<configuration>
<pages masterPageFile="ExampleMaster.master" />
</configuration>
Like any settings specified at the application level, individual pages can elect to override the default masterPageFile attribute, but adding this to your configuration file will guarantee that no pages will be added to your application accidentally without an associated Master Page Nesting of Master Pages
To nest one master page within another page, a process called "Nesting Master Pages". Nesting support is built-in, which means you don't have to do anything special to reap the advantages of nested master pages; a content page that uses a sub-master page will automatically receive content from all the master pages in the hierarchy.Summary
This article explained Master Pages, the new concept in Asp.net 2.0. This would be useful for the beginners of this concept