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.
IN THIS ARTICLE:
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.


Stumble It!
Digg It!
del.icio.us




