| Master Pages |
| |
| Advanced concepts of Master Pages |
| |
| Dynamically Loading Master Page controls in Content pages |
| |
| You might need to co-brand your Web site with one or more partner Web sites. When someone links to your Web site from a partner Web site, you might want to automatically load up a Master Page that matches the look and feel of the partner Web site. |
| |
| In different case, when you want the users of your application to be able to pick a page layout. You can provide your users with a set of standard Master Pages. Users of your application can then select their favorite page layout by selecting their favorite Master Page. |
| |
| You can dynamically load a Master Page by assigning a value to the MasterPageFile property of the Page object. The value assigned to this property should be a relative path to a valid Master Page file. |
| |
| There is one important restriction that you must be aware of when using the MasterPageFile property. You can only assign a value to this property before or during the Page PreInit event. The PreInit is the earliest event during the page execution lifecycle. If you attempt to assign a value to this property during a later event, such as the Page Load event, then you'll receive an exception. This restriction makes sense, since different Master Pages will result in different sets of controls being loaded into the page. |
| |
| To define a Master Page at run-time, simply add the handler for the PreInit even and supply the master page... |
| |
<script runat="server">
Sub Page_PreInit(ByVal sender As Object, ByVal e As EventArgs)
MasterPageFile = myMaster.Master
End Sub
</script> |
| |
| Nesting Master pages |
| |
| Furthermore, if you have the need, you can even nest multiple Master Pages. For example, you can create one Master Page for the entire Web site and then nest Master Pages below the site Master Page for individual sections. |
| |
| Visual Studio/Visual Web Developer does not provide designer support for doing this. So, if you want to nest Master Pages, you'll need to stick to either source code view or build your pages using Notepad. |
| |
| For example, following could be used for a Site Master Page. This Master Page contains the opening and closing HTML and Form tags and a single ContentPlaceHolder control. |
| |
| How to Nest Mater Page |
| |
1. To create nested master page, first create a normal master page and remove all the content in the file except for the directive line.
2. Then create content server control.
3. The object that you place in the content area defined with this Content Control are actually placed in the defined content area with the master page.
4. The ContentPlaceHolderID attribute is tying this content area to the Content Area of ContentPlaceHolder object, which is defined in master page.
5. With in a nested master page, multiple ContentPlaceHolder Server Controls can be used. |
| |
| Sample Code: |
| |
<%@ Master %>
<html>
<head>
<title>Site Master</title>
</head>
<body bgcolor="LightGreen">
<form id="form1" runat="server">
<h1>Site Master Page</h1>
<asp:contentplaceholder
id="SiteContentPlaceHolder"
runat="server" />
</form>
</body>
</html> |
| |
| And the following could be the nested master page. This Master Page overrides the content in the Site Master Page. Notice that the <%@ Master %> directive refers to the SiteMaster.master Master Page. |
| |
| Sample Code: |
| |
<%@ Master MasterPageFile="~/SiteMaster.master" %>
<asp:content ID="Content1"
ContentPlaceHolderID="SiteContentPlaceHolder"
runat="server">
<table width="100%" bgcolor="LightYellow">
<tr>
<td colspan="2">
<h1>Section Master Page</h1>
</td>
</tr>
<tr>
<td>
<asp:ContentPlaceHolder
id="LeftColumn"
Runat="Server" />
</td>
<td>
<asp:ContentPlaceHolder
id="RightColumn"
Runat="Server" />
</td>
</tr>
</table>
</asp:content> |
| |
| Finally, the content is based on the Section Master Page. Notice that this page overrides the two ContentPlaceHolder controls contained in the Section Master Page. |
| |
| Sample Code: |
| |
<%@ Page MasterPageFile="~/SectionMaster.master" %>
<asp:Content ID="Content1"
ContentPlaceHolderId="LeftColumn"
Runat="Server">
This content appears in the left column
</asp:Content>
<asp:Content ID="Content2"
ContentPlaceHolderId="RightColumn"
Runat="Server"> |
| |
| This content appears in the right column |
| |
| </asp:Content> |
| |
| Container (browser) Specific Master Page |
| |
| Depending on viewing container (running browser) using by end user, the Asp.net engine fetch the appropriate master page file. |
| |
| The fist master page file attribute is used for default settings. |
| |
| Sample Code: |
| |
<%@ Page Language=C# MasterPageFile="~/MyMasterPage1.master"
Mozilla: MasterPageFile="~/MyMasterPage2.master"
Opera: MasterPageFile="~/MyMasterPage3.master" %>
<asp:Content ID="Content1" ContentPlaceHolderId="contentplaceholderobj" Runat="Server"> |
| |
| This is Browser specific content. |
| |
| </asp:Content> |
| |
| In this example the fist master page file attribute is used for default settings. Therefore, if the end user is using other then Mozilla or Opera browser, the default master page is MyMasterPage1.master. If the requestor is using Opera browser then MyMasterPage3.master file is used as master page and similarly, MyMasterPage2.master is used for Mozilla browser. |
| |
| Event Ordering |
| |
| For working with master page and content page be sure about which events come before others as both uses the same events. When a user requests a content page in the browser, the event ordering is the following: |
| |
1. Initialize the Master page child controls: The server controls contained within master page are initialized first.
2. Initialize the Content page child controls: The server controls contained within content page are initialized.
3. Initialize the Master page: The master page initialize itself.
4. Initialize the Content page: The content page is initialized.
5. Load the Content page: The content page is loaded.
6. Load the Master page: The master page is loaded.
7. Load the Master page child controls: The server controls contained within master page are loaded onto the page.
8. Load the Content page child controls: The server controls contained within content page are loaded onto the page. |
| |
| Caching with Master Pages |
| |
| To use Output Caching in a master page, put the Output Caching directive in the content page. This caches both the content of content page as well as the content of master page. Since it is not possible to apply caching to just the master page, so you cannot use the Output Caching directive on the master page. On using this, an error will be generated as the application cannot retrieve the cached page. |
| |
|
| |