Access a Method in a Master Page without Code-Behind
Page 1 of 2
Written by
Gregory Scot Collins
Friday, 2 June 2006, 9:32 AM
This article has been tested to work with the following products and versions. No guarantee of compatibility, with or without modification, is offered for products or versions other than those listed.
- ASP.NET 2.0 (Microsoft)
IN THIS ARTICLE:
While working with master pages you might need to access a method located in a master page from a content page. The process, while not intuitive, is not difficult. In this article we'll examine what it takes to access a method in both immediate and parent master pages from a content page, as well as access a method in a parent master page from a child master page. We'll also create some sample files for testing and demonstration.
Please note that the example code in this article does not use code-behind. If you are using code-behind, please refer to Access a Method in a Master Page with Code-Behind.
Basics for accessing a method in an immediate master page
To access a method in the immediate master page from a content page, there are a couple of prerequisites that you need to ensure are in place in the master page. These include:
- Declaring public access for the method you want to call.
- Including the ClassName attribute with a valid value on the Master directive.
Once these prerequisites are in place, you can access a method in a master page from your content page by using the code in Listing 1 and replacing ClassNameAttrValue with the value you provided for the ClassName attribute, and MasterMethod() with the name of the master page method you will be accessing.
You will use the same technique to access a method in a parent master page from a child master page—the child master page being the content page to the parent master page.
Basics for accessing a method in a parent master page
Attempts to use a similar technique to access a method in a parent master page will fail. Since you can access a method in the parent master page from the child master page, you might be tempted to create an intermediary method that calls the methods in the parent master page and returns the results to the content page. Although this approach will work, it is inconvenient and can create a tangle of unnecessary methods.
A cleaner approach for accessing a method in a parent master page requires that you include the MasterType directive in the content page, which once in place, you can access a method in a parent master page from your content page by using the code in Listing 2 and replacing ClassNameAttrValue with the value you provided for the ClassName attribute, and ParentMasterMethod() with the name of the parent master page method you will be accessing.
Note that for the child master page we use Page.Master and for the parent master page we use Page.Master.Master. The MasterType directive gives us access to the parent master page. Without it we would receive the following error message:
Compiler Error Message: CS0246: The type or namespace name 'ClassNameAttrValue' could not be found (are you missing a using directive or an assembly reference?)


Stumble It!
Digg It!
del.icio.us




