Introduction
One of the highlights of ASP.net 2.0 has been the �master pages� feature. Master Pages feature is one of the most power features that many developer have unsuccessfully tried to create alternatives for. Almost none of the development platforms prior to ASP.net 2.0 have ever come close to implementing any thing even close to Master Pages. However powerful and eligant few have indulge into understanding the full impact that Master Pages can bring about. Therefore, in this article we will take a deep dive in the depths of Master Pages.
What are Master Pages:
The idea of Master Pages was taken from PowerPoint. PowerPoint has for years implemented the idea of MasterPages (called Slide Master in Power Point). Basic idea is to reduce replication by putting all elements that are common in one page (master page) and build rest of the pages on top of master page. In programming term think of master page as a virtual base class that can only be used for inheritance purposes. Pacticle example: instead of coping web-page header on every single page, just create the webpage header in master page and then create all the pages on top of (or inherit from) masterpage.
How does it works:
In master pages one can create content place holders. Just as the name suggests, content place holders are place holders for content that will be provided by the pages that are build on top (or inherit) this master page. Example:
In master page: <asp:contentplaceholder id="Text" runat="server"/>
In web page: <asp:content id="Content1" contentplaceholderid="Text" runat="server">Some text.</asp:content>
Key Features of Master Pages:
- Reduces replication: as described earlier, it reduces replication.
- Multiple inheritance or nesting: There can be serveral master pages inheriting from (or nested within) each other. Example Master Page B inherits Master Page A. Therefore, page gamma which directly inherits Master page B also inherits indirectly Master page A. However, there is no design time support for this feature in visual Studio 2005, so expect to hand code this feature. Code example for this:
Master Page A�s page header: <%@ master language="C#" %>
Master Page B�s page header: <%@ master language="C#" masterpagefile="~/MP_A.master"%>
Page Gamma�s page header: <%@ page language="C#" masterpagefile="~/MP_B.master" %>
- Ability to programmatically access elements of master page from regular page�s code behind:
Suppose you are working on an Ecommerce app and the header of page has a cart sub-total user control. The whole header can be put in the master page and the cart sub-total user control can be easily accessed through the page�s code behind. Here is an example for that:
MasterPage m = Page.Master;
Cart c = (Cart)m.FindControl("oCart");
c.SubTotal = 45.33;
- Ability to specify default content: If a web page does not provide content for one or more of the content place holder(s) then the default content is used. The default content is placed in between the content place holder tags in the master page.
Conclusion:
As you see this is really a powerful feature. One most obvious use for this feature is in the Ecommerce technology. And from when I have seen around the web so far alim systems have used this technology to the fullest. Hope you have learned from this episode of the ASP.net 2.0 Highlights. Until next time, take care.