Search
Web Braintrove
Site Navigation
Home
Products
Tags
Levels
Dates
Authors
5 MOST RECENT
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
Preview and Open Forms after Upgrading to Internet Explorer 8
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
Use Logical Operators in Place of Conditional Expressions
Books
Test-Driven Development in Microsoft .NET
MCAD/MCSD Training Guide (70-315): Developing and Implementing Web Applications with Visual C# and Visual Studio.NET
Microsoft Visual C# .NET Step by Step
ASP.NET 2.0 Instant Results
Beginning ASP.NET 2.0
Magazines
.net: The Internet Magazine
SQL Server Magazine
Inside Web Design
Inside Visual Basic
.net
DVDs
Visually Learn Visual Basic .NET in 12 Hours
Programming Visual Basic .NET and ADO.NET with SQL Server and Access
Beginner's ASP.NET in C# 2003
Beginner's ASP.NET in VB.NET 2003
Beginner's .NET XML Web Services 2004
Microsoft MVP
PopularMost Viewed
This article has been viewed 3013 times.

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.
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:
  1. Declaring public access for the method you want to call.
  2. 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?)
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 Child.master file.
Listing 5 - The Default.aspx file.
See Also
Access a Method in a Master Page with 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.