Search
Web Braintrove
Site Navigation
Home
Products
Tags
Levels
Dates
Authors
5 MOST RECENT
Locate the Design Checker Task Pane in InfoPath 2010
Locate Conditional Formatting and Custom Validation in InfoPath 2010
Conditionally Display Different Values In a Single Expression Box
Create a Button With a Blank Label
Create Shared Rules
5 MOST POPULAR
Access a Method in a Master Page with Code-Behind
Pass Query String Parameters to an ASP.NET Xml Control
Prevent Namespace Prefixes from Being Copied to the Output
Create Groups in a Drop-Down List Box
Configure IIS 7 for Custom Error Pages
5 MOST FORGOTTEN
Install Windows 7 Beta onto a Virtual Machine
Group Dates by Year, Month and Day
Configure Lab Color Mode Defaults for Braintrove Articles
Create a Monochromatic Dream Text Effect Using Styles
Combine Multiple Sums Into a Single Function
Books
Sams Teach Yourself ASP.NET 2.0 in 24 Hours, Complete Starter Kit
Programming Microsoft ASP.NET 2.0 Applications: Advanced Topics
Professional Web Parts and Custom Controls with ASP.NET 2.0
Microsoft Visual C# .NET Step by Step
Build Your Own ASP.NET Website Using C# & VB.NET
Magazines
Net Developers Journal
.net
Visual Studio Magazine
Soa Web Services Journal
Web Site Management And Internet Advertising Trends
DVDs
Beginner's .NET XML Web Services 2004
Programming Visual Basic .NET and ADO.NET with SQL Server and Access
Visually Learn Visual Basic .NET in 12 Hours
Beginner's ASP.NET in VB.NET 2003
Beginner's ASP.NET in C# 2003
Microsoft MVP
PopularMost Viewed
This article has been viewed 3182 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.