Tuesday, May 14, 2013

Application State in Asp.net


Introduction

The Application object is used to store and access variables from any page, just like the Session object. The difference between "Application and Session" is that In Application, all user share one Application object and In Sessions, there is one Session object for each user.

The first time a user requests a page that resides in an application's directory, ASP.NET initializes the application. During that process, ASP.NET creates an application object from the HttpApplication  class. This object is represented by a special class file named global.aspx.
The application object can be accessed by any of the application's pages. 

Application state is typically used to store application-specific data that changes infrequently. In other word The information can also be changed in one place, and the changes will automatically be reflected on all pages.

How to work with Application events

you may be wondering how to initialize the values of application state items. To do that, you first add a Global.asax file to the project. By default, this file contains method declarations for five event handler. like below




When an web page load, this event occurs only one time in an application’s life cycle. It occurs again when you restart the IIS. When IIS reset then application count also reset to zero (0). It’s the best place to count number of visitors.

Program Code

global.asax


<%@ Application Language="C#" %>
<script runat="server">
     void Application_Start(object sender, EventArgs e) 
    {
        // Code that runs on application startup
       Application["PageHit"] = 0;
    } 
</script>

VisitorPage.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="VisitorCount.aspx.cs" Inherits="VisitorCount" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <h3 style="color: #006600">Visit page</h3>
    <div>
    <asp:Label ID="countlbl" runat="server" Text="Label" Font-Bold="True" Font-Size="Large"></asp:Label>
        <asp:Button ID="Button1" runat="server" Text="Next Page" OnClick="Button1_Click" style="z-index: 1; left: 9px; top: 101px; position: absolute; height: 26px" />
    </div>
    </form>
</body>
</html>
VisitorPage.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
 
public partial class VisitorCount : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Application["PageHit"] = Convert.ToInt32(Application["PageHit"].ToString()) + 1;
        }
        countlbl.Text = "Page hit -> " + Convert.ToInt32(Application["PageHit"].ToString());
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        Response.Redirect("Page2.aspx");
    }
}

Page2.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Page2.aspx.cs" Inherits="Page2" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <h3 style="color: #0000FF">Page 2</h3>
    <div>
    <asp:Label ID="countlbl" runat="server" Text="Label" Font-Bold="True" Font-Size="Large"></asp:Label>
    </div>
        <br />
        <asp:Button ID="Button1" runat="server" Font-Bold="False" OnClick="Button1_Click" Text="Back" />
    </form>
</body>
</html>
Page2.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
 
public partial class Page2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        countlbl.Text = "Page hit ->  " + Convert.ToInt32(Application["PageHit"].ToString());
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        Response.Redirect("VisitorCount.aspx");
    }
}
Output 1


click on Next Page button

Output 2
Now you will see page count does not change but redirect on "Page2.aspx" page.

Now click on Back button.

Output 3
 you will see page count has increase by 1.



Output 4
Reload or Refresh the "VisitorPage.aspx" page. you will see page count has increase by 1 for every time.