Access a Method in a Master Page with Code-Behind
Page 1 of 3
Written by
Gregory Scot Collins
Friday, 16 June 2006, 7:09 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:
- Basics for accessing a method in an immediate master page
- Basics for accessing a method in a parent master page
- Bring it all together
- Create the Parent.master file
- Create the Parent.master.cs file
- Create the Child.master file
- Create the Child.master.cs file
- Create the Default.aspx file
- Create the Default.aspx.cs file
- Try it
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 uses code-behind. If you are not using code-behind, please refer to Access a Method in a Master Page without 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 you need to ensure that in the master page code-behind file you declare public access for the method you want to call. Having done this, you can access a method in a master page from your content page by using the code in Listing 1 and replacing ClassName with the name of your code-behind class, 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 ClassName with the name of your code-behind class, 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 'ClassName' could not be found (are you missing a using directive or an assembly reference?)


Stumble It!
Digg It!
del.icio.us




