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.
2.3.2. What just happened?
There are two ways you can modify the content of a Master page during runtime:
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.