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
Recover a Dark Picture with a Heavy Color Cast
Hide Deleted Items in the Source Control Explorer
Prevent Windows Vista from Requesting Your Permission to Continue
Create a Variable with a Unique Sorted Node-Set
Get the Oldest or Newest Date Value of a List Node
Microsoft MVP
PopularMost Viewed
This article has been viewed 3969 times.

Access a Method in Global.asax with Code-Behind

Page 1 of 2
Written by Gregory Scot Collins
Wednesday, 21 June 2006, 4:28 PM
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.
    You might find it necessary at some point to have a global method—one that is maintained in a single location and accessible from any number of pages. Although it appears that this practice is highly discouraged, there comes a point where it is more practical than trying to maintain multiple copies of the same code across numerous pages. A great place to store this global method is in the Global.asax file.
    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 Global.asax without Code-Behind.

    Basics for accessing a method in Global.asax

    To access a method in Global.asax from a content page you need to ensure that in the Global.asax code-behind file you declare public access for the global method you want to call. Having done this, you can access a method in Global.asax from your content page by using the code in Listing 1 and replacing ClassName with the name of your code-behind class, and GlobalMethod() with the name of the global method you will be accessing.

    Bring it all together

    Let's create four test files: the Global.asax file and a content page—each with their own associated code-behind file. These files will demonstrate how to access a global method in Global.asax from the content page.
    Because we will be using Active Server Pages, you will need access to a .NET enabled Web server. We will assume that you have one installed locally and that you will be testing this on http://localhost. If you are working with a remote server, substitute in your server name and any required path.
    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 the Global.asax file.
    Listing 2 - The Global.asax file.
    Listing 3 - The Global.asax.cs file.
    Listing 4 - The Default.aspx file.
    Listing 5 - The Default.aspx.cs file.
    See Also
    Access a Method in Global.asax without Code-Behind
    Access a Method in a Master Page with Code-Behind
    Access a Method in a Master Page without Code-Behind
    Track the Active Visitor Count of a Web Site
    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.