Search
Web Braintrove
Site Navigation
Home
Products
Tags
Levels
Dates
Authors
5 MOST RECENT
Create Shared Rules
Perform a Case-Insensitive String Comparison without Using Translate()
Force a Field Value to a Boolean Using Rules
Quickly Crop to a Shadow
Sum Repeating Time Values
5 MOST POPULAR
Pass Query String Parameters to an ASP.NET Xml Control
Access a Method in a Master Page with Code-Behind
Prevent Namespace Prefixes from Being Copied to the Output
Create Groups in a Drop-Down List Box
Change the Default Action of the Power Button on the Windows Vista Start Menu
5 MOST FORGOTTEN
Install Windows 7 Beta onto a Virtual Machine
Configure Lab Color Mode Defaults for Braintrove Articles
Permanently Change the Default Styles for New Word Documents
Enable Themes in Windows Server
Conditionally Display the Singular or Plural Form of Words
Books
Beginning ASP .NET 2.0 E-Commerce in C# 2005: From Novice to Professional
Beginning ASP.NET 2.0 in C# 2005: From Novice to Professional
Microsoft ADO.NET
Mastering Web Development with Microsoft Visual Studio 2005
Murach's ASP.NET 2.0 Upgrader's Guide: VB Edition
Magazines
Visual Basic & Net Journal
SQL Server Magazine
Asp.netpro
Inside Visual Basic
Web Site Management And Internet Advertising Trends
DVDs
Visually Learn Visual Basic .NET in 12 Hours
Beginner's ASP.NET in C# 2003
Programming Visual Basic .NET and ADO.NET with SQL Server and Access
Beginner's .NET XML Web Services 2004
Beginner's ASP.NET in VB.NET 2003
Microsoft MVP
Very PopularMost Viewed
This article has been viewed 10077 times.

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.
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?)
Bookmark this Article
StumbleUpon  Stumble It!
Digg  Digg It!
del.icio.us  del.icio.us
List of Figures
Listing 1 - Code for accessing a method in an immediate master page.
Listing 2 - Code for accessing a method in a parent master page.
Listing 3 - The Parent.master file.
Listing 4 - The Parent.master.cs file.
Listing 5 - The Child.master file.
Listing 6 - The Child.master.cs file.
Listing 7 - The Default.aspx file.
Listing 8 - The Default.aspx.cs file.
See Also
Access a Method in a Master Page without Code-Behind
Access a Method in Global.asax without Code-Behind
Access a Method in Global.asax with Code-Behind
Article Tags
Great Deals
TigerDirect Exclusive Deals, Limited Time Offers, Act Now And Save!
Find all current special offers on Adobe products.
Try SugarSync Free!
Join WebHost4Life.com
TigerDirect
Computers4SURE (4SURE.com - An Office Depot Co.)
Copyright © 2006-2010 Braintrove. All rights reserved. Braintrove, braintrove.com, and the Braintrove logo are trademarks of Gregory Scot Collins in the United States and/or other countries. The names of actual companies and products mentioned herein may be the trademarks of their respective owners. Any rights not expressly granted herein are reserved.