Section 2.3. Modify a Master Page at Runtime


2.3. Modify a Master Page at Runtime


Note: You can flexibly modify the content of the Master page so that it suits the theme of the page.

When a Web Form that uses a Master page is loaded at runtime, it displays the content of the Master page together with its own content. However, there are times when you will want to modify parts of the Master page when a particular web page is loaded. For example, at the O'Reilly Network site (http://www.oreillynet.com), pages that belong to the ONDotnet subsite display the ONDotnet logo (a leaping dolphin) in their headers, rather than the generic O'Reilly Network logo (the ever-familiar tarsier). Pages at other subsites, such as Perl.com and ONLamp.com, do likewise.

2.3.1. How do I do that?

To see how a Master page can be modified on the fly, we'll add a page to our previous project (see Section 2.2). This page will use MasterPage.master and change the image in the Master page from the O'Reilly Network logo to the ONDotnet logo when it is loaded.

  1. Using the project created in the previous lab, add a new Web Form (right-click the project name in Solution Explorer, select Add New Item..., and then select Web Form) and check the "Select master page" checkbox at the bottom of the dialog. Name the Web Form ONDotnet.aspx. Click Add.

  2. Select Masterpage.master as the Master page.

  3. Populate the ONDotnet.aspx Web Form with text (see Figure 2-14).

    Figure 2-14. The ONDotnet.aspx Web Form with text added


  4. Switch to Source View and insert the MasterType directive after the Page directive. The MasterType directive allows you to indicate a strongly typed reference to the Master page so that you can programmatically access its content (such as its public properties and methods):

    <%@ Page Language="VB" MasterPageFile="~/MasterPage.master"      AutoEventWireup="false" CodeFile="ONDotnet.aspx.vb"      Inherits="ONDotnet_aspx" title="Untitled Page" %> <%@ MasterType VirtualPath="~/MasterPage.master" %> ...

  5. In the code-behind of the Master page (MasterPage.master.vb), insert the public setLinkColor( ) method. By adding this public method to the Master page, Content pages will be able to programmatically set the color of the various LinkButton controls:

    Imports System.Drawing Partial Class MasterPage_master     Inherits System.Web.UI.MasterPage     Public Sub setLinkColor(ByVal ctrl As String, _                             ByVal lnkColor As Color)         Select Case ctrl             Case "MacDevCenter"                 lnkMacDevCenter.ForeColor = lnkColor             Case "ONDotnet"                 lnkONDotnet.ForeColor = lnkColor             Case "MozillaDevCenter"                 lnkMozillaDevCenter.ForeColor = lnkColor             Case "ONLamp"                 lnkONLamp.ForeColor = lnkColor         End Select     End Sub End Class


    Note: You need to use the Public access modifier on the setLinkColor( ) method, or else the Content page will not be able to call it.

  6. In the code-behind of ONDotnet.aspx (ONDotnet.aspx.vb), code the following Page_Load event:

    Sub Page_Load(ByVal sender As Object, _               ByVal e As System.EventArgs) _               Handles Me.Load     '---change the link color     Master.setLinkColor("ONDotnet", Drawing.Color.Yellow)     '---change the title     Master.Page.Header.Title = _        "O'Reilly and Associates Conferences"     '---change the image     Dim masterImage As Image     masterImage = CType(Master.FindControl("imgTitle"), _                   Image)     If Not (masterImage Is Nothing) Then         masterImage.ImageUrl = "Images/ondotnet_logo.gif"     End If End Sub

  7. Essentially, you use the Master property to get a reference to the Master page. You first change the color of the ONDotnet.com LinkButton control by invoking the setLinkColor( ) method. You then change the title of the page by using the Master.Page.Header.Title property. Finally, you change the image of the logo in the Master page by using the FindControl( ) method.


    Tip: Master is a special property exposed by the Web Form as a handler to access the Master page. In fact, every page can access the Master property; however it makes sense only if a page uses a Master page.

  8. Press F5 to test the application. First load Default.aspx and then click the ONDotnet.com link to load ONDotnet.aspx. Figure 2-15 shows the differences between the two pages.

    Figure 2-15. Viewing the Default.aspx and ONDotnet.aspx pages


2.3.2. What just happened?

There are two ways you can modify the content of a Master page during runtime:

  • Calling the public methods and setting the public properties of the Master page

  • Using the FindControl( ) method (of the Master property) to locate controls on the page, and then calling the methods and setting the properties of those controls


Warning: The Master property is valid only on pages that reference a Master page in the MasterPageFile attribute in the Page directive. If you access the Master property on a page that does not reference a master page, a NullReferenceException is thrown.

Where possible, you should use the first method, which exposes the public methods and properties of the Master page. This approach is a much more efficient way to change controls on a Master page because it uses early binding, and therefore the controls are strongly typed.

To use early binding, the Master page needs to expose its public methods and properties (as in Step 5) so that a Content page can set or access them during runtime. In this lab you have exposed a public method called setLinkColor( ) in the MasterPage.master page. To change the color of a LinkButton in the Master page during runtime, you can call the setLinkColor( ) method in the Page_Load event of a Content page.


Note: Use early binding whenever possible; it makes your development much easier and less error-prone.

With the second method, you locate the controls you want to modify on the Master page by using the FindControl( ) method of the Master property, then supplying the name and type of the control you want to modify. Once the control is located, you can change its properties as if it were a local object, as demonstrated in the following code fragment, which locates an image control on the Master page and substitutes a new .gif file containing the ONDotnet logo.

Dim masterImage As Image masterImage = CType(Master.FindControl("imgTitle"), _               Image) If Not (masterImage Is Nothing) Then     masterImage.ImageUrl = "Images/ondotnet_logo.gif" End If

This technique uses late binding to access controls on a Master page because you need to explicitly convert the controls you find during runtime to the type that you want.


Note: The FindControl( ) method uses late binding, while using public properties has the advantage of early binding and hence is strongly typed.

2.3.3. What about...

...accessing controls that are dynamically created when the Master page loads?

For example, a Master page may display additional late-breaking news by dynamically creating Label controls. The number of new controls generated may vary depending on the urgency of the news.

The advantage of making methods and properties public on a Master page is that you can use early binding, which means you can use IntelliSense to help you with their names and you can rely on the compiler to check their types at compile time.

However, if there are controls that will be created dynamically in the Master page during runtime, exposing public properties is no longer a viable option, since you need to decide what methods and properties to expose at design time.

In this case, you should resort to late binding using the FindControl( ) method, described earlier in "What just happened?".

2.3.4. Where can I learn more?

To learn more about early and late binding and the advantages and disadvantages of each, visit the following MSDN Help topic: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbcn7/html/vaconearlylatebinding.asp.



ASP. NET 2.0(c) A Developer's Notebook 2005
ASP. NET 2.0(c) A Developer's Notebook 2005
ISBN: N/A
EAN: N/A
Year: 2005
Pages: 104

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net