Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 Global.asax file

2 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

3 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

4 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

5 5 Global.asax file.

6 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.

7 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.

8 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

9 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()); }

10 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(" Welcome to my website! " ); Response.Write(" This is my header that comes from Application level " ); Response.Write(" ");}

11 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(" "); Response.Write("Copyright 2002-" + dateStr ); Response.Write("This is my customer footer that from Application level" ); Response.Write(" "); }

12 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(); }

13 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); }

14 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()); }

15 15 Check out for the Demo

16 16 Session Useful methods: Session.Clear() – Clears all values from session, but leaves session active Session.Abandon() – Ends the current session. Read session Session(“message”) = “Hello” Set session txtHello.text = Session(“message”)


Download ppt "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."

Similar presentations


Ads by Google