Click here to monitor SSC
  • Av rating:
  • Total votes: 143
  • Total comments: 29
Dan Wahlin

ASP.NET Master Pages Tips and Tricks

14 June 2007

With the release of ASP.NET 2.0, developers were given a simple and effective way to apply a consistent layout across multiple pages in a website. By creating a file with a .master extension that defined a website's overall layout template and referencing it with the Page directive's MasterPageFile attribute, website development and maintenance took a step forward in the direction of greater productivity.

Master pages have been around for over one and a half years, so I won't cover the fundamentals of creating and using them, as many tutorials and books have already been written about the topic. Instead, I'll focus on a few tips and tricks that can be applied when using master pages. To start, let's examine how the MasterType directive can be used to reference master page controls in a strongly-typed manner from a content page.

Using the MasterType Directive

Pages that reference controls in a master page, such as a Label in the header, or Menu on the left or right of a website template, typically do so by using the Page class's Master property along with the FindControl() method, as shown in Figure 1.

Label lbl = this.Master.Page.FindControl("lblHeader") as Label;
if (lbl != null)
{
lbl.Text = "Welcome from the content page!";
}

Figure 1: controls in a master page can be accessed by using the Master property combined with the FindControl() method.

While this approach certainly works, any misspellings in the quotes will not be caught by the compiler, resulting in a runtime error or a null object reference being returned. Fortunately, a strongly-typed solution is available that doesn't involve casting the Master property to the base class of the master page in order to access its members (keep in mind that any server controls defined in the master page won't be accessible even after a cast is performed, because they're marked as protected by default).

In cases where a control defined in a master page needs to be exposed to one or more content pages in a strongly-typed manner, a public property with a get block can be added into the master page class as shown in Figure 2. The get block returns a Label control instance named lblHeader.

public Label HeaderLabel
{
    get { return lblHeader; }
}

Figure 2: exposing a Label control in a master page through a public property.

A content page can reference members defined in the custom master page class by adding the MasterType directive immediately under the Page directive:

<%@ MasterType VirtualPath="~/Templates/WebsiteMasterPage.master" %>

This causes the ASP.NET compiler to use the custom master page class for the type of the Page class's Master property as opposed to the default MasterPage class located in the System.Web.UI namespace. As a result, the public property defined in the master page can be directly accessed from the content page in a strongly-typed manner, as shown in Figure 3.

protected void Page_Load(object sender, EventArgs e)
{
    this.Master.HeaderLabel.Text = "Label updated using MasterType " +
      "directive with VirtualPath attribute.";
}

Figure 3: accessing a master page's public property from a content page by using the MasterType directive.

Using the MasterType property not only results in less code being written across multiple content pages, but also leads to better performance and eliminates the need to pass quoted values to FindControl().

Creating Master Page Base Classes

Developers who need to dynamically change master pages on the fly during the Page's PreInit event will quickly discover that using the MasterType, along with the VirtualPath attribute, will not work. This is because the VirtualPath value is 'hard coded' into the content page. However, another solution exists that can be used in situations where multiple master pages are in play.

In cases where the same public property must be defined in multiple master pages (such as a Label control in a website header), a base master page class can be created that derives from MasterPage, as shown in Figure 4. This class can be added in the App_Code folder.

public abstract class BaseMasterPage : MasterPage
{
    public abstract Label HeaderLabel
    {
        get;
    }
}

Figure 4: creating a custom master page file class with a single public abstract property.

By defining the BaseMasterPage class as abstract, it can't be created directly and can only serve as the base for another class. By defining the HeaderLabel property as abstract, master pages that derive from BaseMasterPage must provide an implementation for the property. Figure 5 shows an example of deriving a master page class from BaseMasterPage and implementing the abstract HeaderLabel property.

public partial class Templates_InheritedMasterPage : BaseMasterPage

{

    public override Label HeaderLabel

    {

        get { return lblHeader; }

    }

 

 

    protected void Page_Load(object sender, EventArgs e)

    {

        //Provide default text in case content page doesn't set any

        if (String.IsNullOrEmpty(lblHeader.Text))

        {

            this.lblHeader.Text = DateTime.Now.ToLongDateString();

        }

    }

}

Figure 5: deriving from BaseMasterPage and implementing an abstract property.

Pages that need to access the HeaderLabel property, but don't want to reference a specific master page using the MasterType's VirtualPath attribute, can use the TypeName attribute instead, as shown next:

<%@ MasterType TypeName="BaseMasterPage" %>

The compiler will apply the class defined by the TypeName attribute to the Page class's Master property allowing strongly-typed access to the HeaderLabel property from a content page, as shown earlier in Figure 3. The downside of this approach is that any custom controls defined in a concrete master page class won't be accessible through Intellisense™ and will have to be accessed using FindControl(). However, any master page that derives from BaseMasterPage will expose a HeaderLabel property, allowing master pages to be dynamically loaded in PreInit and used. This technique can of course be used in more advanced scenarios where multiple controls need to be exposed to content pages.

Handling Nested Master Page Design Issues

Master pages can be nested inside of other master pages in cases where an overall site's layout template needs to contain a child template (see the sample code for an example of nesting master pages). While nesting master pages is useful in some situations, it presents a problem when trying to use the Visual Studio .NET 2005 design surface to drag and drop controls onto a content page. This problem is resolved in the next release of VS.NET (currently called Orcas).

There are a few different ways to get around the nested master page design-time issue. One potential solution is to temporarily change the MasterPageFile attribute's value to empty strings on the Page directive. Although you won't be able to see how the layout template defined in the master page looks when combined with the content page, you'll be able to drag and drop controls onto the content page while in design view. However, you'll have to remember to update the MasterPageFile attribute with the proper master page file path before moving the page to test or production environments.

Another solution is to leverage a lesser known aspect of the Page directive. Custom properties defined in an ASP.NET's code-behind class can be referenced in the Page directive as attributes (I first learned about this trick from Microsoft's Scott Guthrie). This feature can be used to provide a run-time reference to a master page and get around the VS.NET nested master page designer issue, as no master page is defined until the page is actually run.

Figure 6 shows a base class named BasePage that derives from System.Web.UI.Page and defines a RuntimeMasterPageFile property. BasePage overrides the Page's PreInit event and dynamically assigns the MasterPageFile property to the value contained in the RunTimeMasterPageFile property.

public class BasePage : System.Web.UI.Page {

    private string _RuntimeMasterPageFile;

    public string RuntimeMasterPageFile {
        get {
            return _RuntimeMasterPageFile;
        }
        set {
            _RuntimeMasterPageFile = value;
        }
    }

    protected override void OnPreInit(EventArgs e) {
        if (!String.IsNullOrEmpty(RuntimeMasterPageFile)) {
            this.MasterPageFile = RuntimeMasterPageFile;
        }
        base.OnPreInit(e);
    }
}

Figure 6: creating a base class that derives from Page and defines a single property named RuntimeMasterPageFile. This property is used to specify the master page file that should be used at runtime.

A page that derives from BasePage can then assign the MasterPageFile attribute of the Page directive to empty strings (to avoid the designer issue mentioned earlier) but then define the master page file that should be used at runtime by adding a RuntimeMasterPageFile attribute as shown next:

<%@ Page AutoEventWireup="true" 
  CodeFile="WorkingWithNestedMasterAndBasePage.aspx.cs"
  CodeFileBaseClass="BasePage"
  Inherits="WorkingWithNestedMasterAndBasePage" Language="C#"
  MasterPageFile=""
  RuntimeMasterPageFile="~/Templates/NestedMasterPage.master"
  Title="Nested Master Page Demo" %>

Defining the RuntimeMasterPageFile attribute will cause the associated property in BasePage to be assigned a value which is then used during PreInit to assign a value to the MasterPageFile property. Although every page that references a nested master page has to derive from BasePage for this trick to work, it's one potential solution to nested master pages that prevents having to temporarily remove the MasterPageFile attribute value to edit a content page in design view.

Sharing Master Pages across IIS Applications

The MasterPage class available in ASP.NET 2.0 derives from UserControl and just like user controls, master pages can't be shared across IIS applications. There are a few different solutions that have been proposed, such as setting up virtual directories in each IIS application that point to the same physical folder, but there is a way to share master pages across applications with a little work on your part without resorting to duplicating virtual directories across multiple websites. By leveraging the VS.NET 2005 Publish Web Site tool it's possible to create an assembly that contains all of the master page HTML code and C# or VB.NET code, give the assembly a strong name, and install it into the Global Assembly Cache (GAC).

I consider this trick more of a hack, but it's something you can try out if/when the situation requires it. There are several steps involved, so a step-by-step approach follows, as well as issues to watch out for when performing the steps. I originally wrote about this some time ago on my blog at http://weblogs.asp.net/dwahlin.

  1. Create an empty Website in VS.NET 2005. Delete everything in it including App_Data, Default.aspx, and web.config (if it exists).
  2. Add a master page into the website. A simple master page file is shown in Figure 7.
  3. <%@ Master Language="C#" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
        <title>Untitled Page</title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            Header
            <br />
            <asp:contentplaceholder id="ContentPlaceHolder1" runat="server">
            </asp:contentplaceholder>
            <br />
            Footer
        </div>
        </form>
    </body>
    </html>

    Figure 7: a simple master page file that defines a ContentPlaceHolder control.

  4. Select Build | Publish Website from the VS.NET menu.
  5.  On the screen that follows, select a target location, and check all of the checkboxes shown in Figure 8. Note that the image shown in Figure 8 references a strong name key file named keyfile.snk that was created using the sn.exe command-line tool that ships with .NET. This is required in order to install assemblies into the GAC. The following syntax can be used to create the key file (run it using the Visual Studio .NET 2005 command prompt): sn.exe -k keyfile.snk.
  6. Figure 8: using the Publish Web Site tool to create an assembly from a master page.

  7. After the publish operation completes, open the new website in VS.NET 2005 (named MasterDemo in the example above). You should see a new assembly (with a strange name) in the Bin folder. This assembly is your master page in compiled form.
  8. Install the assembly into the GAC using gacutil.exe or drag-and-drop it into c:\Windows\Assembly using Windows Explorer. Once you've done this, delete the original assembly as well as the newly created XML files associated with it from the website.
  9. Add a web.config file into the website and add the following within the <system.web> begin and end tags.
  10. <compilation debug="true">
       <assemblies>
         <add assembly="App_Web_masterpagebase.master.cdcab7d2, Version=0.0.0.0,
            Culture=neutral, PublicKeyToken=cceb8435cfc68486" />
       </assemblies>
    </compilation>

    You'll need to change the name of the assembly to the name that is generated for your project (the one you added into the GAC) and change the PublicKeyToken to the one you see in the GAC. Note that the assembly attribute value shouldn't wrap at all. I didn't give my base master page a version for simplicity, but you can do so by applying the [assembly: AssemblyVersion("1.0.0.0")] attribute to the master page code-behind class. Note that if you're using the Web Application Project feature of VS.NET 2005 you can give your master page assembly a friendlier name. I'll leave that as an exercise for the reader.

  11. Add a master page into the website, but don't create a code-behind page for it (you can, but it's not needed in this case since the master page is only used to reference the one installed in the GAC).
  12. Remove all code within the new master page and add the following at the top. It should be the only code in the page.
  13. <%@ Master Language="C#" Inherits="ASP.masterpagebase_master" %>

    If you named the original master page (the one created in step 2) differently, then you'll need to change the Inherits value. Use the VS.NET object browser to see the name of the class within the .dll generated in step 4.

  14. Create a content page that references MasterPage.master (the one you created in the previous step). The default ContentPlaceHolderID is ContentPlaceHolder1, so use that in the <asp:Content> tag unless you gave the id a different name in step 2.

After completing these steps, any IIS application can share the same master page used by other IIS applications by placing the empty MasterPage.master file into the application and updating the web.config file to point to the master page assembly in the GAC. The downfall of this approach is that you have to recompile the base master page and put it back into the GAC each time you need to make a change and the design time support is lacking. There may be other issues as well that haven't been discovered yet, so perform proper testing before assuming this technique will work for your particular situation.

In working with more complex master pages you may see a 'could not find string resource' error come back when using this approach. If you use Reflector (http://www.aisto.com/roeder/dotnet) to analyze the code generated when the master page is compiled into the assembly, you'll likely see a call to a method named CreateResourceBasedLiteralControl() in the code rather than seeing the actual HTML from the master page being embedded into the assembly. If you strip out some of the whitespace in the HTML it should eliminate the call to CreateResourceBasedLiteralControl() that the Publish Web Site tool added and compile correctly. For example, change the following:

<td align="left" valign="top" height="45">
    <www:HtmlOutput ID="egovHeader" runat="Server"
        XmlSource="/XML/SiteLinks.xml"
     XsltSource="/XSLT/Header.xslt"
     LanguageCookieName="EgovCookie/Language" />              
</td>

To the following (all in one line):

<td align="left" valign="top" height="45"><www:HtmlOutput ID="egovHeader"
runat="Server" XmlSource="/XML/SiteLinks.xml" XsltSource="/XSLT/Header.xslt"
LanguageCookieName="EgovCookie/Language" /></td>

If you try this technique and continue to get a string resource error, you'll probably have to play around with your HTML in the base master page until no CreateResourceBasedLiteralControl() calls are made in the generated code. As mentioned before, I would advise using Reflector to take a look.

Conclusion

Master pages provide a great way to apply a consistent layout to an ASP.NET 2.0 website, resulting in better productivity and reduced maintenance. In this article you've seen various tips and tricks that can be used with master pages such as accessing master page controls in a strongly-typed manner using the MasterType directive, creating base master page classes, and working with nested master pages. You've also seen one potential technique for sharing master pages across IIS applications. Additional samples are available with this article's downloadable code.

Dan Wahlin

Author profile:

Dan Wahlin (Microsoft Most Valuable Professional for ASP.NET and XML Web Services) is a .NET development instructor at Interface Technical Training (http://www.interfaceTraining.com). Dan founded the XML for ASP.NET Developers Web site (www.XMLforASP.NET), which focuses on using XML, ADO.NET and Web Services in Microsoft’s .NET platform. He’s also on the INETA Speaker's Bureau and speaks at several conferences. Dan has co-authored/authored several different books on .NET including ASP.NET 2.0 MVP Hacks and XML for ASP.NET Developers (Sams). When he’s not writing code, articles or books, Dan enjoys writing and recording music and playing golf and basketball with his wife and kids. Dan blogs at http://weblogs.asp.net/dwahlin and http://blogs.interfacett.com/dan-wahlins-blog.

Search for other articles by Dan Wahlin

Rate this article:   Avg rating: from a total of 143 votes.


Poor

OK

Good

Great

Must read
Have Your Say
Do you have an opinion on this article? Then add your comment below:
You must be logged in to post to this forum

Click here to log in.


Subject: Great article
Posted by: Habtamu (not signed in)
Posted on: Monday, June 18, 2007 at 2:22 PM
Message: I appreciate ur effort ,keep up the good work!!

Subject: MasterType in UserControls
Posted by: Anonymous (not signed in)
Posted on: Friday, June 22, 2007 at 4:17 AM
Message: So how do we get the same functionality in USerControls, I've tried the MasterType page directive in a UserControl and it doesn't support that particular directive, so is there a nice clean way of getting the same functionality in UserControls?

Subject: Great article
Posted by: Barbod (not signed in)
Posted on: Sunday, June 24, 2007 at 11:25 PM
Message: Really tanx , it's so cool and great article...

Subject: Please give me sample code on Remoting.
Posted by: piyush (view profile)
Posted on: Wednesday, June 27, 2007 at 1:16 AM
Message: Please give me sample code on Remoting.

Subject: excellent
Posted by: Tom Regan (not signed in)
Posted on: Friday, June 29, 2007 at 6:21 AM
Message: Excellent article, thank you for posting it.

Subject: MasterPages
Posted by: Ted M. (not signed in)
Posted on: Friday, June 29, 2007 at 9:08 AM
Message: Hi,
In nearly every example for using MasterPages shows methods to access an object from the content page, as in figure 1 and 3 of this article, where the value of the label is set from within the content page. But nowhere can I find a VB example of accessing the "value" of an object in a MasterPage from the content page. For example, have a dropdownlist and a button in the MasterPage. If an item is selected in the DDL and the button is pressed, have the value of the DDL displayed in a textbox inside the content page. This cross-visibility between MasterPages and content pages would make the MasterPages even more powerful... if I knew how to do it.

thanks

Subject: Master Page name mangling
Posted by: Ben M (not signed in)
Posted on: Friday, June 29, 2007 at 9:47 AM
Message: The biggest issue I found with master pages is that the ID of the controls in the page get mangled. Instead of txtFirstName you end up with something like 0d8j_Content_txtFirstName (where content is the ID of the ContentPlaceHolder). That can obviously cause problems if you want to use CSS and JavaScript libraries that reference the IDs of elements. I found a solution where you set the ID of the master page itself in the Page_Init event. This way, I end up with IDs such as Master_Content_txtFirstName. That's not ideal, but at least it's consistent.

I also found that VS.NET really doesn't want me to put a ContentPlaceHolder within the head tag. That could be very useful for adding page specific META tags and CSS.

I was very excited about master pages and I admit that they are very useful, but I just find them a little half baked. Go look at the Ruby on Rails implementation and you'll see what I mean.

Subject: Masterpages in the GAC(gacutil.exe vs drag-and-drop)
Posted by: cg (not signed in)
Posted on: Monday, July 09, 2007 at 4:25 PM
Message: Using the drag-and-drop method to add assemblies to the GAC did NOT work for me. I kept getting FileNotFound exceptions when the content page was loading the assemblies.

After spending a few hours troubleshooting, I decided to try the gacutil.exe to add the assemblies. Worked like a charm!

Thanks Dan, very useful.

Subject: Get error
Posted by: Sonny (not signed in)
Posted on: Friday, July 27, 2007 at 7:24 PM
Message: I get the following error when I have more then 4 div tags embedded.

An error occurred while try to load the string resources (FindResource failed with error -2147023083).

this works fine

<div id="page">
<div id="page-a">
<div id="page-b">
<!-- Main Page Container -->
<!-- A. HEADER -->
<div class="header">

</div>
</div>
</div>
</div>

this does not, it gives me the top error

<div id="page">
<div id="page-a">
<div id="page-b">
<!-- Main Page Container -->
<!-- A. HEADER -->
<div class="header">
<div class="header-top">
test
</div>
</div>
</div>
</div>
</div>


Any thoughts?

Subject: MasterType in UserControls
Posted by: Daniel M (not signed in)
Posted on: Wednesday, September 05, 2007 at 9:55 PM
Message: Actually I have the same question like in the second comment. Please tell us if there is a way to access exposed properties from the MasterPage in a UserControl?
Thank you!

Subject: regarding master pages
Posted by: satish (not signed in)
Posted on: Thursday, October 04, 2007 at 6:43 AM
Message: Hello sir i am satish i ahve some problem regarding master pages, please send me using masterpager demo website ....

Subject: This article
Posted by: RLM (not signed in)
Posted on: Tuesday, October 09, 2007 at 7:08 AM
Message: Excellent article, thanks for taking the time to explain in intermediate-advanced language so as not to waste time

RLM
Orlando, FL

Subject: Not Using the GAC
Posted by: Carlos Cysneiros (not signed in)
Posted on: Wednesday, October 10, 2007 at 9:24 AM
Message: Can I use the assemblie without having to put it in the GAC. I would like for individual applications to consume the a Web Application Project .dll Can I reference it in the MasterPage itself or do I have to put it on the Web.config file?

Subject: Master Page access from UserControl
Posted by: Anonymous (not signed in)
Posted on: Wednesday, November 21, 2007 at 9:56 AM
Message: So I'll be the 3rd guy to ask this question. If you don't know, just say "I don't know".

Subject: About article
Posted by: Ravikiran (not signed in)
Posted on: Wednesday, November 28, 2007 at 4:28 AM
Message: This article is quite informative for me thank you..

Subject: Master Page access from UserControl
Posted by: Deepak (not signed in)
Posted on: Monday, December 03, 2007 at 12:38 AM
Message: Ok this is 4th guy looking for the answer !

Subject: FindResource failed with error -2147023083
Posted by: Matthew Sannes (not signed in)
Posted on: Wednesday, December 05, 2007 at 10:37 AM
Message: For anybody else that is having this problem, if you install SP1 for the .NET 2.0 framework, exception goes away. Hope this helps some people out.

Subject: Setting Master Page Property Of Child master page Dynamically
Posted by: Sachin (view profile)
Posted on: Monday, January 07, 2008 at 7:10 AM
Message: how to set Master Page Property Of Child master page Dynamically in nested master page.

Subject: invoke methos on contents from master page
Posted by: Anonymous (not signed in)
Posted on: Wednesday, January 30, 2008 at 7:15 PM
Message: How to invoke a method in a page(content) from a button that is in the master.

Subject: Thanks for the help!
Posted by: Randy Morter (not signed in)
Posted on: Friday, February 15, 2008 at 10:22 AM
Message: Your tip on adding the MasterType directive solved a big problem for me. Thanks!

Subject: Really Greate Article !
Posted by: Nilesh India (not signed in)
Posted on: Tuesday, April 01, 2008 at 9:44 AM
Message: I read all the article before comming to this article. It is really Excellant and Very Practical.

Subject: Sharing Master Pages across IIS Applications - Content unclear
Posted by: Archna (not signed in)
Posted on: Thursday, April 10, 2008 at 8:55 AM
Message: In point 6- which assembly will be deleted? The original version or the published version?
In point 7- Where do we add the web.config file? Orginal version or published version?

Subject: Getting System.ArgumentException.
Posted by: Anubhuti (not signed in)
Posted on: Monday, April 14, 2008 at 10:43 AM
Message: The debugger takes me to autogenerated code

Protected Overrides Sub FrameworkInitialize()
MyBase.FrameworkInitialize
Me.__BuildControlTree(Me)
End Sub

And Get above error in line
MyBase.FrameworkInitialize
'Value does not fall within the expected range'.
I am doing exactly same steps as above

Subject: used actual dll insead of gac
Posted by: swingman (view profile)
Posted on: Thursday, October 16, 2008 at 7:08 PM
Message: First of all i used asp:literal to solve the CreateResourceBasedLiteralControl() problem.

Right now my current setup is having to share the dll created at the root of the applications.

By that i can just share the same root on sourcesafe instead of people installing it at the gac. Our devs can now add the reference and change the inheritance from System.Web.UI.MasterPage to ASP.masterpage_master at the codebehind of the blank masterpage.

So as you can see I didnt remove the code behind of the blank masterpage as the post suggests. which brings me to my quiestion.

Based on my setup, is there a more effecient way to add the blank masterpage without having the code behind and change the inheritance?

maybe just a .cs file or is there a way to inherit straight from the .Master file w/o the code behind files?

Subject: where's vb code ?
Posted by: agunghario (view profile)
Posted on: Sunday, June 20, 2010 at 11:10 PM
Message: may i can get on vb.net code ?

Subject: Try a converted
Posted by: BPrashanthSimple (view profile)
Posted on: Tuesday, December 07, 2010 at 12:41 AM
Message: There are bunch of online converters that can help you convert VB.net to C# and vice-versa

Prashanth
http://adarshsoft.com

Subject: Try a Converted
Posted by: BPrashanthSimple (view profile)
Posted on: Tuesday, December 07, 2010 at 12:42 AM
Message: Sorry abt the typo

Prash
<a href="http://adarshsoft.com">Adarsh</a>

Subject: ASP.NET Master Pages Tips and Tricks
Posted by: candyford (view profile)
Posted on: Thursday, January 27, 2011 at 5:48 AM
Message: Hi Don,
In Asp.net 2.o, Master pages are the main new controls plays an important role while comparing to Asp.net 1.0.Master pages are very useful and used in each and every website very frequently. Thanks for chosing this topic and your solution is also very good. Thanks again for sharing such an useful info with us.

http://www.gloriatech.com/microsoft-net-development-services.aspx

Subject: Cast error when using technique outlined in 'Using the MasterType Directive'
Posted by: dotnetuser (view profile)
Posted on: Monday, October 24, 2011 at 11:11 AM
Message: I've tried the technique outlined in 'Using the MaterType Directive' in my project but am getting the following error:
'Unable to cast object of type 'ASP.NameOfMasterPage' to type 'NamespaceOfWebsite'

This error occurs when I try to open the Content page i.e. the page doesn't open

The following is what I have done in my project:

In the code behind of the parent Master page:
public HtmlGenericControl BodyTag
{
get
{
return bdMasterPageBodyTag;
}
set
{
bdMasterPageBodyTag = value;
}
}

Under the page directive in the Content page:
<%--<%@ MasterType VirtualPath="~/Site.Master" %>--%>

I declare a class variable in the Content page code behind:
HtmlGenericControl bdMasterPageBodyTag;

In the Page_Load event of the Content page I initialise that class variable like so:
bdMasterPageBodyTag = this.Master.BodyTag;

I haven't added anything to the nested Master page.

Can someone see what I have done wrong?

Thanks in advance

 

Top Rated

Acceptance Testing with FitNesse: Multiplicities and Comparisons
 FitNesse is one of the most popular tools for unit testing since it is designed with a Wiki-style... Read more...

Acceptance Testing with FitNesse: Symbols, Variables and Code-behind Styles
 Although FitNesse can be used as a generic automated testing tool for both applications and databases,... Read more...

Acceptance Testing with FitNesse: Documentation and Infrastructure
 FitNesse is a popular general-purpose wiki-based framework for writing acceptance tests for software... Read more...

TortoiseSVN and Subversion Cookbook Part 11: Subversion and Oracle
 It is only recently that the tools have existed to make source-control easy for database developers.... Read more...

TortoiseSVN and Subversion Cookbook Part 10: Extending the reach of Subversion
 Subversion provides a good way of source-controlling a database, but many operations are best done from... Read more...

Most Viewed

A Complete URL Rewriting Solution for ASP.NET 2.0
 Ever wondered whether it's possible to create neater URLS, free of bulky Query String parameters?... Read more...

Visual Studio Setup - projects and custom actions
 This article describes the kinds of custom actions that can be used in your Visual Studio setup project. Read more...

.NET Application Architecture: the Data Access Layer
 Find out how to design a robust data access layer for your .NET applications. Read more...

Calling Cross Domain Web Services in AJAX
 The latest craze for mashups involves making cross-domain calls to Web Services from APIs made publicly... Read more...

Web Parts in ASP.NET 2.0
 Most Web Parts implementations allow users to create a single portal page where they can personalize... Read more...

Why Join

Over 400,000 Microsoft professionals subscribe to the Simple-Talk technical journal. Join today, it's fast, simple, free and secure.