ASP.NET Creating Nested Master Pages
In this tutorial you will learn how master pages can be nested within one main master page, build and example nested master page with the help of sample code.
In the preceding sections we had seen how to create simple master and content pages. We also examined how we can reference members of a master page from content pages and how to use different masters with a single content page. In this section we shall see how master pages can be nested within one main master page.
When one master page references another as its master, it is said to be a nested master page. A number of nested masters can be componentized into a single master. The advantage of this kind of structuring is that a number of child masters can be created to be subordinated to the overall look and feel of the site defined by the Parent master, while the child master give the child pages some uniqueness. While a parent master defines the overall layout of the pages—header, body and footer, the child master expands the body for a group of pages.
Child masters have the extension .master too. It contains all the controls that are mapped to content place holders on the parent master page. The layout is similar to that of a content page in this respect. However, child masters also have content place holders of their own to display content of its child pages.
There is no architectural limitation to the number of child masters that can be created. The depth of nesting also does not impact on performance significantly.
The design support view does not support the creation of nested masters. Developers will have to use source view or note pad to create nested masters.
Building an example:
We shall now do an exercise of creating a master called sampletutorial, a nested master called ExForSystut master and a content file called About.aspx. Follow the steps detailed in the earlier sections and create the two masters and the content page. Name the first master sampletutorial.master and in code view enter the following code.
Now name the second master ExForSystut.master and enter the following code
Now create an aspx file called About.aspx and enter the following code.
Now build the web application and see the output. The output will appear like the one displayed below.
Note that the About.aspx content has been loaded into ExForSystut.Master and that in turn has been loaded into the sampletutorial.master. Note that different contentplaceholders on the main master can be used to load groups of content files loaded into the child masters which are inturn loaded into the main master. This implies that multiple child masters can load multiple content files into different contentplaceholders on the main master file.
ASP.NET 2.0 is not the first in support of templating. However, the functionality provided by this software goes beyond the frontiers of templating to make master pages components that can be reused. The mechanism is not based on class inheritance. The binding of the master and the content pages is defined statically and cannot be changed or set programmatically.
In the next tutorial we shall be looking at another component provided by ASP.NET that promotes reusability—Web Parts. Web Parts content rich, modular and customizable components that help the developer make cutting edge web sites.