Master Pages in ASP.NET 2.0
Overview
In ASP.NET 1.1 to give your application a coherent style you were using user controls to make one footer and one header, maybe you make one menu, then you drop those controls into each and every new page you add to your application , it's somehow a good way to make a consistenet maintainable UI. Master Pages are an easier solution !
What are Master Pages ?
Master pages lets you make a consistent layout for your application , you can make one master page that holds the layout/look & feel and common functionality of your whole application and upon this master page you can build all the other pages, we call these pages Content Pages, So simply you can build your master page and then build content pages , and while creating the content pages you bind the content pages to the master page you have created before, those two pages are merged at runtime to give you the rendered page.
Master Pages and ContentPlaceHolders
The master page has the extension .master and it's actually a aspx page but with the directive <%@ Master Language="C#"%> , instead of the standard page directive, almost the attributes of the Master directive are the same as that of the page, you can add any kind of controls the same as you design aspx pages, and it's advisable to use the menu conrol at your master page ( menu is one of the most nice added conrols to ASP.NET 2.0) .Every MasterPage should include at least one ContentPlaceHolder, this Control is a placeholder for the content that will be merged at runtime, noting that the content page is just an aspx standard page but you should supply the attribute MasterPageFile to the page directive to bind the content page to the master page , for example
<%@ Page Language="C#" MasterPageFile="~/MasterPages/SiteLayout.master"%>
Again you can design your content page just like any other aspx page adding any static text and server controls.
Content Server Conrtol
Inside the content pages you will find one Content server control added by default, actually when you add controls you add them to the content server control, for example
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
the attribute ContentPlaceHolderID is very important as it decides that this content will be bound to which ContentPlaceHolder on the master page , this is a very nice way to decide the location of where the contents will be displayed; so this way you can have multiple content on one content page and also multiple ContentPlaceHolders on the master page, how nice !.
Note : the content pages don't include the common tags as <Body>, <Head>, remember that that was the same with user controls , as after merging there should be only one Body and Head tags.
Programmable Master Pages
Master Pages are programmable you can make one property to the master page and it will be visible to all the content pages, for example you can get one string like last build or last update from web.config file and provide it as a public property to all the content pages.
Maintainble Master Pages
The best thing about master pages is maintainability as you can now update on master page and this will update all your application pages that reference this master page.
<< Home