What is Global.asax File?
The global.asax allows us to write event handlers that react to global events in web applications. Global.asax files are never called directly by the user, rather they are called automatically in response to application events.
Point to remember about global.asax:-
1. They do not contain any HTML or ASP.NET tags.
2. Contain methods with specific predefined names.
3. They defined methods for a single class, application class.
4. They are optional, but a web application has no more than one global.asax file.
5. global.asax file aren't attached in the same way as the event handlers for ordinary control events.
6. Way to attach them is to use the recognized method name, i.e. for event handler Application_OnEndRequest(), ASP.NET automatically calls this method when the HttpApplication.EndRequest event occurs.
Form1.html
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Global
Events Demo</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h1>Global
Events</h1>
<asp:Button ID="btnEndSession" runat="server" Text="End
Session" onclick="btnEndSession_Click" />
<asp:Button ID="btnError" runat="server" Text="Generate
Error" onclick="btnError_Click" />
</div>
</form>
</body>
</html>
btnEndSession_Click
protected void btnEndSession_Click(object sender, EventArgs e)
{
Session.Abandon();
}
protected void btnError_Click(object sender, EventArgs e)
{
int a
= 5;
int b
= 0;
int c=a
/ b;
}
Events that don't get fired with every request
1.Application_Start():-This method is invoked when the application first starts up and the application domain is created. This event handler is a useful place to provide application-wide initialization code. For example, at this point you might load and cache data that will not change throughout the lifetime of an application, such as navigation trees, static product catalogs, and so on.
2.Session_Start():-This method is invoked each time a new session begins. This is often used to initialize user-specific information.
3.Application_Error():-This method is invoked whenever an unhandled exception occurs in the application.
4.Session_End():-This method is invoked whenever the user's session ends. A session ends when your code explicitly releases it or when it times out after there have been no more requests received within a given timeout period (typically 20 minutes).
5.Application_End():-This method is invoked just before an application ends. The end of an application can occur because IIS is being restarted or because the application is transitioning to a new application domain in response to updated files or the process recycling settings.
6.Application_Disposed():-This method is invoked some time after the application has been shut down and the .NET garbage collector is about to reclaim the memory it occupies. This point is too late to perform critical cleanup, but you can use it as a last-ditch fail-safe to verify that critical resources are released.
https://www.youtube.com/channel/UCKLRUr6U5OFeu7FLOpQ-FSw/videos
0 comments
Post a Comment