An In-Depth Coverage of ASP.NET 2.0's Master Pages: Part 2 of 3
Specifying a Master Page
You can specify the master page for a content page in two different ways:
- At the page level: In the content page's @Page directive, you can specify the master page. This is the method used when you create a content page through Visual Studio. A sample page directive is:
<%@ Page MasterPageFile="~/MySite.master". %>
- At the application or folder level: You also can specify the master page to use for content pages in the web.config file. For this, use the web.config file's pages element's masterPageFile attribute. By using this method, one can specify in one place the master page for all the content pages in the Web site or in a particular folder. A sample setting is:
<configuration>
<system.web>
<pages masterPageFile="~/MySite.master" />
</system.web>
</configuration>
Nested Master Pages
Accessing Controls on the Master Page
The following code in the content page's code-behind file shows an example of using FindControl:
Visual Basic
' Gets a reference to a Label control ("masterPageLabel")
' on the master page
Dim mpLabel As Label
mpLabel = CType(Master.FindControl("masterPageLabel"), Label)
If Not mpLabel Is Nothing Then
'Set content page title to master page control
Title.Text = mpLabel.Text
End If
C#
// Gets a reference to a Label control ("masterPageLabel")
// on the master page
Label mpLabel = (Label) Master.FindControl("masterPageLabel");
if(mpLabel != null)
{
//Set content page title to master page control
Title.Text = mpLabel.Text
}
Accessing Methods and Properties of the Master Page
The Master property is defined in this way:
Visual Basic
Public ReadOnly Property Master As MasterPage
C#
public MasterPage Master { get; }
Home.aspx
<%@ Page MasterPageFile="~/SiteMaster.master"
CodeFile="Home.aspx.cs"
Inherits="Home" %>
<%@ MasterType VirtualPath="~/SiteMaster.master" %>
Home.aspx.cs
this.Title = Master.MpProperty; // No type-casting required.
A sample application for accessing master page controls and methods and properties is included.
Setting Master Page Programmatically
Visual Basic
Sub Page_PreInit(ByVal sender As Object, _
ByVal e As EventArgs) Handles Me.PreInit
Me.MasterPageFile = "~/NewMaster.master"
End Sub
C#
void Page_PreInit(Object sender, EventArgs e)
{
this.MasterPageFile = "~/NewMaster.master";
}
Referencing External Resources
There are two different ways in which paths are handled in master pages:
- Server controls: In server controls on master pages, ASP.NET modifies the URLs of properties that reference external resources. For example, in the above application, if you have an Image Web control on the master page SiteMaster.master and you set its ImageUrl property to "Images/logo.jpg" at runtime ASP.NET will modify the URL so that it resolves correctly in the context of the content page. The behavior is summarized below.
<asp:Image ID="Image1"
runat="server"
ImageUrl="Images/logo.jpg" />Runtime rendering for Home.aspx:
<img ID="Image1" src="Images/logo.jpg" />
Runtime rendering for AdminTest.aspx:
<img ID="Img1" src="../Images/logo.jpg" />
Result: Image renders just fine for both Home.aspx and AdminTest.aspx.
- Plain html: If you have plain HTML elements (that is, HTML controls without the runat="server" specified) on the master page, ASP.NET does not modify this and passes it on as is. This can create problems. For example, consider a plain HTML img on the master page as shown.
<img ID="Image2" src="Images/logo.jpg" />
Runtime rendering for Home.aspx:
<img ID="Img2" src="Images/logo.jpg" />
Runtime rendering for AdminTest.aspx:
<img ID="Img3" src="Images/logo.jpg" />
Result: Image shows up in Home.aspx but not for AdminTest.aspx because the path is incorrect.
- Instead of using plain HTML, use server controls. You can do this by using corresponding Web server controls or by putting runat="server" in the plain HTML, thereby making them HTML server controls. So, in the above example you could change the plain img tags to
<asp:Image ID="Image1" ImageUrl="Images/logo.jpg"
runat="server" /><img ID="Img4" src="Images/logo.jpg" runat="server" >
- In the master page, for the plain HTML elements, instead of using a relative path, give the full path. This method doesn't result in a performance hit but the drawback here is that it's not a good idea to hardcode the URLs to the full path. So, in the example, the img would be specified as
<img ID="Img5"
src="http://www.mywebsite.com/Images/logo.jpg" > - Keep your file layout in such a way that the content pages have the same relative position to the master page.
No comments:
Post a Comment