|
|||||||||||||||||||||
|
|||||||||||||||||||||
|
Announcements
Want a new Job?
Chapters
Services
Feature Zones
|
IntroductionMaster pages are templates for ASP.NET pages. It helps you to create consistence in appearance for your web site with minimum code effort. The great thing about master pages is that they are live templates, you can change them from within the content pages. They are not only for visual consistency but can also act as a global function library. Any function declared in the master page is viewable in the content pages. Create master pageCreating master page is very simple. From Visual Studio Solution Explorer:
Adding controls to a Master pageMaster pages are much like normal aspx pages but they don’t have the <%@ Master Language="C#" AutoEventWireup="true"
CodeFile="myMasterPage.master.cs" Inherits="myMasterPage" %>
Also the extension of the master pages is “.Master” and make sure that you don’t change this extension because this prevents the pages from being browsed by browsers directly. I’ll explain this later in this article. Now you are free to add any controls or code to your master page. You can add navigation controls such as You should note that by default when you create a master page it contains only one control. It’s the <asp:contentplaceholder id="ContentPlaceHolder1"
runat="server">
</asp:contentplaceholder>
Note:All code snippets in this article are in C#. For full VB.NET code, please refer to the attached source code. Creating Content pagesTo create a Content page from Visual Studio, in the Solution Explorer:
When you open the web from in the Visual Studio designer, you notice that the controls of the master page appear but you can’t edit them. When you view the source of the content page, it looks like this. <%@ Page Language="C#" MasterPageFile="~/MasterPages/myMasterPage.master"
AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
</asp:Content>
The link between the content page and master page is in the attribute Now try to add anything to the content page and browse it. You will see the contents of two pages together. NoteCan I browse master pages directly? No, you can’t because the .master extension is associated with Master-Content relation is not inheritanceAfter having a quick overview about master pages, it’s time to dig through it internally. It might seem that the relation between a Master and a Content page is “Inheritance” like what happens in Windows Forms visual inheritance. But in fact it’s far from the truth. Master pages are actually user controls. When you see the code file of your master page, you will find that it inherits the class The complete family tree is shown in the image below: What actually happens when the user requests a web form in ASP.NET 2.0 is:
private void PerformPreInit()
{
this.OnPreInit(EventArgs.Empty);
this.InitializeThemes();
this.ApplyMasterPage();
this._preInitWorkComplete = true;
}
Runtime sends the output of merged pages to the client. How controls are arranged in the outputThe master page is working as a container for the controls inside it. If you examine the trace output of this page, you will find something like:
TreeView tv=(TreeView) this.FindControl(“TreeView1”);
it will not succeed. How to interact with master page controls from content pageThe next question is “How to change something in the master page from the content page?”. To do that, the product team changed the public MasterPage Master
{
get{}
}
It returns an instance of the
Calling functions from the master pageWe can use the master pages not only as visual templates but we can also use them as function libraries where we can add the most common, UI-related functions. But take care to not add a lot of code because it will be loaded with each and every page in your web site. Let’s say that we have a function called public string MasterFunction()
{
return "this is returned from master page";
}
We can simply call it with this code snippet. //make sure that you added the MasterType directive.
myMasterPage myMaster =this.Master;
lblTest.Text = myMaster.MasterFunction();
When is my code executed?That’s a very important question to ask, when you put code in the master and content pages’
I did a nice code sample that demonstrates the event order. Take a look at the code attached with this article. Master pages don’t support themesMaster page don’t support themes simply because the void PerformPreInit()
{
this.OnPreInit(EventArgs.Empty);
this.InitializeThemes();
this.ApplyMasterPage();
this._preInitWorkComplete = true;
}
But if the content page has a theme, it will be applied on the whole output later after merging the master and content pages. Take care of URLsIf we have an image control on the master page (or any control that references URLs), and we have a relative URL (something like images/myImage.gif) which is relative to the place of the master page in the web site, but when the master page is merged in the content of the content page, what will be the situation? Don’t worry about that, the ASP.NET runtime will take care of converting any URLs to the appropriate ones. But take care of non-server-control tags like the Adding a Master page to the whole site at onceIf you have a single master page that you wish to add to all your web forms without specifying the <pages masterPageFile="mySite.master" />
Nested Master PageMaster pages can be nested. You can use this feature to distribute your master pages design among more than one master page. The only defect using this strategy is that Visual Studio doesn’t support nested master pages. So you have to work without Visual Studio designer. Note:Any master page that has another master page should be treated like the content page so it can not have anything outside the Let’s say that you have a page called nestedMasters.aspx which has a master page called childMaster.master and this master page has another master page called topMaster.master. The code of the topMaster.master would be like this: <%@ Master Language="C#" AutoEventWireup="true"
CodeFile="topMaster.master.cs" Inherits="topMaster" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title id=pageTitle runat="server">Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table width="100%">
<tr width=100%>
<td width=100% bgcolor="#99ff99">This is the parent master page<br />
<asp:Label ID="label1" runat="server" Text="Label" Width="351px"></asp:Label>
</td>
</tr>
<tr width=100%>
<td width=100%><asp:contentplaceholder id="ContentPlaceHolder1" runat="server">
</asp:contentplaceholder></td>
</tr>
</table>
</div>
</form>
</body>
</html>
And the childMaster.master code is: <%@ Master Language="C#" AutoEventWireup="true" CodeFile="childMaster.master.cs"
Inherits="childMaster" MasterPageFile="~/MasterPages/topMaster.master" %>
<asp:Content ID=content1 runat="server" ContentPlaceHolderID=ContentPlaceHolder1>
<table width=100%>
<tr width=100%>
<td width=100% bgcolor="#00ccff"> This is child master page<br />
<asp:Label ID=label1 runat="server" Text=Label />
</td>
</tr>
<tr width=100%>
<td width=100%><asp:ContentPlaceHolder ID=contentPlaceHoder1
runat="server" EnableViewState=true></asp:ContentPlaceHolder></td>
</tr>
</table>
</asp:Content>
And the nestedMasters.aspx should be like this: <%@ Page Language="C#" MasterPageFile="~/MasterPages/childMaster.master"
AutoEventWireup="true" CodeFile="nestedMasters.aspx.cs"
Inherits="nestedMasters"
Title="Untitled Page" %>
<asp:Content ID="Content1" ContentPlaceHolderID="contentPlaceHoder1"
Runat="Server">This is content page
</asp:Content>
Creating nested master pages is very simple. But when it comes to interaction between content page and master page, it needs us to pay attention to some issues. Arrangement of controls in the output page is very important to know if you want to deal with nested master pages. Take a look at this image of page tracing: Note that the content page is the container for the This will lead us to another question, What about the interaction between the content page and the master pages? The only visible class to the content page is the direct master page. The content page can’t create a reference of the type of any master page other than its direct master page. So you can’t use them directly from the content page. To overcome this, I found two ways to do this. Using Properties/MethodsYou can use properties or methods only in the Let’s say I want to change two controls:
In the public void changeLocalControl(string controlName, string newValue)
{
if (controlName == "label1")
{
label1.Text = newValue;
}
}public void changeMasterControl(string controlName, string newValue)
{
topMaster myMaster = (topMaster)this.Master;
if (controlName == "pageTitle")
{
HtmlTitle title = myMaster.FindControl("pageTitle") as HtmlTitle;
if(title !=null)
title.Text=newValue;
}
}
In the In the Using FindControl from the content page directlyIf you are in a hurry and you don’t want to bother yourself with writing properties or methods in the According to the trace output earlier, all controls on To get a control in the Label lbl = myChildMaster.Master.FindControl("label1") as Label;
lbl.Text = "testing";
To get a control in the
Now take a look at the trace output and check this code snippet below: Button btn =
myChildMaster.Master.FindControl("ContentPlaceHolder1").FindControl("button1")
as Button;
btn.Text = "by the content page";
In this code, I get a reference to the I hope I could shed light on Master Pages and I’m waiting for your feedback :) | ||||||||||||||||||||