The Application object is a built-in ASP.NET
object that represents an instance of the ASP.NET
application. It includes all the methods and
collections related to the application.When the
first user calls an ASP.NET file, the application
is started and an Application object is created.
Once the Application object is created, it can
share information throughout the application. The
object remains until the application is shut
down. The Application object stores and maintain
values through variables. Variables can have two
level of scope:
- Page Level Variables: Page Level
variables are used while the page is being
processed. After the page is processed, the
variables are destroyed and the associated
resources are released.
Code Snippet:
String user_name="Mak";
Response.write("Hello "+user_name);
- Object Level Variables: Object Level variables and their values can be accessed across pages. There are two types of Object Level variables:
- Application Level variables are used to share common information across multiple users.
- Session Level variables are used to pass information from one page to another for a particular user.
| ||
|
Global.asax
protected void
Application_Start(object sender, EventArgs
e) { Application["count"] = 0; } protected void Session_Start(object sender, EventArgs e) { Response.Write("Session Started"+"<br />"); Application["count"] = Convert.ToInt32(Application["count"]) + 1; } protected void Application_BeginRequest(object sender, EventArgs e) { Response.Write("Application Request Begins" + "<br />"); } protected void Application_EndRequest(object sender, EventArgs e) { Response.Write("Application Request ends"); } protected void Application_AuthenticateRequest(object sender, EventArgs e) { } protected void Application_Error(object sender, EventArgs e) { } protected void Session_End(object sender, EventArgs e) { Response.Write("Session Ended"); } protected void Application_End(object sender, EventArgs e) { } |
ApplicationObj.aspx
<script language="C#"
runat="server"> protected void Page_Load(object sender, EventArgs e) { Response.Write("Visitor Number:" + Application["count"] + <br>); } </script> |
When the user starts the application, the Application_Start event is fired.
Output:
Application Request Begins
Session Started Visitor Number:1 Application Request ends |
If user refresh the page, the counter is not incremented as shown below.
Application Request Begins
Visitor Number:1 Application Request ends |
If .aspx page is loaded in a new browser window, the counter is incremented by one as shown below.
Application Request Begins
Session Started Visitor Number:2 Application Request ends |
An application variable retains the value assigned to it until the application is shut down.
No comments:
Post a Comment