Using Application Event Handlers In The Globalasax File

You have eighteen supported application events in ASP.NET Applications for writing application-specific code. You also have the capability to create you own custom application level events. Typically, you will use the built-in events. You can use any of these built-in events by implementing the event handlers for these events in the Global.asax file. This is done in the Global.asax file with the following naming convention

Application_EventName( appropriateevent argumentsignature).

Usually, you will add parameters to get more contextual information on the event. This is a typical event handler signature that includes parameters —

Application_EventName (Object sender, EventArgs e). The sender tells you who raised this event and the EventArgs gives you further context about why the event was raised. The sender and EventArgs are not required parameters of the event handler, but it is good practice to include these parameters.

Some applications' events are raised for each request to the application, and others just occur under special conditions. For example, you can also use the

BeginRequest and EndRequest events for code that you want to execute each time a user makes a request. Place this in the Global.asax file between the <SCRIPT> tags.

USING THE BEGINREQUEST AND ENDREQUEST METHODS

Untitled Notepad

File Edit Format Help lew Ctrl+N

Untitled Notepad

File Edit Format Help lew Ctrl+N

i Open...

Ctrl+O

Save

Ctrl+S

Save As...

% Untitled Notepad

File Edit Format Help

i/SCRIRT>

□ Start the text editor to edit the Global.asax file.

-0 Click File O Open.

□ Click Open to open the template.

□ Start the text editor to edit the Global.asax file.

-0 Click File O Open.

Navigate to the Code Templates directory and open the DeclareServer SideObjectGlobal Template.asax file.

□ Click Open to open the template.

Note: You can open another Global.asax file if you want.

string stringSiteName = "My Lifetime Goals"; Application["applicationSiteName"] - stringSiteName;

void Applioation_End() [

Application["applicationSiteName"] - "";

void Session_Start() {

Re5ponse.Write("<FuNT FACE-Verdana'>Session Start event handler called.. cBR/sc/FONTs");

string stringUserGreeting - "Welcome to My Lifetime Goals", SessionrsessionUserGreeting"] = stringUserGreeting;

void Session_End() {

SessionrsessionUserNickname"] ="";

void Application_BeginRequest(Object sender, EventArgs E) {

Response.Write("«FONT FACE-Verdana'>Begin Reguest event handier called.. cBR/ac/FONTa");_

void Application_EndRequest(Object sender, EventArgs E) {

Response Write("=;FONT FACE-Verdana-End Reguest event handler called... <BF;/></FONT>");_

i/SCRIRT>

To create the event handler for request, click between the <SCRIPT> and </SCRIPT> tags, type void Application_BeginRequest() { , and press Enter.

Type } to finish the event handler and press Enter.

Q To create the event handler to end the request, click between the <SCRIPT> and </SCRIPT> tags, type void Application_EndRequest() { , and press Enter.

^O Press Tab to indent, type } to finish the event handler, and press Enter.

Was this article helpful?

0 0

Post a comment