global.asax file. agenda what is global.asax file how to add the global.asax file what are the...

16
Global.asax file

Upload: regina-pearson

Post on 13-Jan-2016

213 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Global.asax file. Agenda What is Global.asax file How to add the Global.asax file What are the default events available Explanation to Application_Level

Global.asax file

Page 2: Global.asax file. Agenda What is Global.asax file How to add the Global.asax file What are the default events available Explanation to Application_Level

Agenda

• What is Global.asax file

• How to add the Global.asax file

• What are the default events available

• Explanation to Application_Level Events

• Example 1 :To display Server time

• Example 2:To Create Customized header

• Example 3:To display the Session ID

• Example 4: To display the number of hits

Page 3: Global.asax file. Agenda What is Global.asax file How to add the Global.asax file What are the default events available Explanation to Application_Level

3

What is Global.asax file?

• It is also known as the ASP.NET application

file

• It is an optional file with VS 2005 editor

• What does it consists of?

– It consists of the code for responding to application

level and session level events raised by ASP.NET

– ASP.NET is configured such that any direct URL

request to global.asax file is rejected

Page 4: Global.asax file. Agenda What is Global.asax file How to add the Global.asax file What are the default events available Explanation to Application_Level

4

How to add?

• Its simple , Right click on the solution explorer in visual studio editor -> Add New Item -> Select "Global Application Class" - > press "Add" Button

Page 5: Global.asax file. Agenda What is Global.asax file How to add the Global.asax file What are the default events available Explanation to Application_Level

5

Global.asax file.

Page 6: Global.asax file. Agenda What is Global.asax file How to add the Global.asax file What are the default events available Explanation to Application_Level

6

Application Level Events• Application_Start - This method will get

fired when First time the application and web server (eg. IIS ) starts, That means If the IIS recycle or restart again then this event also will be fired.

• Application_End - This method will be fired when When application started shut down.

Page 7: Global.asax file. Agenda What is Global.asax file How to add the Global.asax file What are the default events available Explanation to Application_Level

7

Application Level Events

• Application_Error - This event  will be fired when there is any exception occurred in the application during run time. This event method is one of most important method used by most of the web developer.

• Session_Start -  This event will be fired when a new user request a page from the server. That means for every user request the page this event will be fired.

• Session_End - This code will be fired when session end for a use.

Page 8: Global.asax file. Agenda What is Global.asax file How to add the Global.asax file What are the default events available Explanation to Application_Level

8

Application Level Events

• Application_OnBeginRequest - This event  will be fired for each request the application receives,just before the page is executed

• Application_OnEndRequest - This event  will be fired for each request the application receives,just after the page is executed

Page 9: Global.asax file. Agenda What is Global.asax file How to add the Global.asax file What are the default events available Explanation to Application_Level

9

Example 1

• To display the server time globally throughout all the web forms using the Application_OnBeginRequest event

void Application_OnBeginRequest(object sender, EventArgs e) { Response.Write("This page was servered at" + DateTime.Now.ToString()); }

Page 10: Global.asax file. Agenda What is Global.asax file How to add the Global.asax file What are the default events available Explanation to Application_Level

10

Example 2

• To display a custom header and footer using the Application_OnBeginRequest and Application_OnEndRequest

protected void Application_BeginRequest(Object sender, EventArgs e){ Response.Write("<H1> Welcome to my website! </H1>" ); Response.Write(" This is my header that comes from Application level " ); Response.Write("<HR>");}

Page 11: Global.asax file. Agenda What is Global.asax file How to add the Global.asax file What are the default events available Explanation to Application_Level

11

Example 2 Contd..

• Customized footer

protected void Application_EndRequest(Object sender, EventArgs e){ int yearDate ; string dateStr; yearDate = System.DateTime.Now.Year; dateStr = yearDate.ToString(); Response.Write("<HR>"); Response.Write("Copyright 2002-" + dateStr ); Response.Write("This is my customer footer that from Application level" ); Response.Write("<HR>");}

Page 12: Global.asax file. Agenda What is Global.asax file How to add the Global.asax file What are the default events available Explanation to Application_Level

12

Example 3

• To display the unique session Id for each user using the Session_Start Event void Session_Start(object sender, EventArgs e) { Session.Add("SomeValue", Session.SessionID); }

protected void Page_Load(object sender, EventArgs e) { Label1.Text = "Your Session ID is" + Session["SomeValue"].ToString(); }

Page 13: Global.asax file. Agenda What is Global.asax file How to add the Global.asax file What are the default events available Explanation to Application_Level

13

Example 4

• To display the number of hits or the usercount

• Initialise the user count

void Application_Start(object sender, EventArgs e) { Application.Add("userCount", 0);

}

Page 14: Global.asax file. Agenda What is Global.asax file How to add the Global.asax file What are the default events available Explanation to Application_Level

14

Increment the counter and display in the webform

void Session_Start(object sender, EventArgs e) { int userCount = int.Parse(Application.Get("userCount").ToString()); userCount++; Application.Set("userCount", userCount);

}

protected void Page_Load(object sender, EventArgs e) { Page.Response.Write("Usercount:" + Application.Get("userCount").ToString()); }

Page 15: Global.asax file. Agenda What is Global.asax file How to add the Global.asax file What are the default events available Explanation to Application_Level

15

Check out for the Demo

Page 16: Global.asax file. Agenda What is Global.asax file How to add the Global.asax file What are the default events available Explanation to Application_Level

16

Session

Useful methods:Session.Clear() – Clears all values from session, but leaves session activeSession.Abandon() – Ends the current session.Read session

Session(“message”) = “Hello”Set session

txtHello.text = Session(“message”)