USER VISIT IN A WEBSITE (USING APPLICATION ASAX CLASS)

Hint:
need to add global.asax file.to do this
right Click on your website name from Solution explorer -> Add New Item -> Global Application Class. It will be added in the root directory of your website with the name "Global.asax".
Open the global.aspx file and write the code on the Application_Start, Session_Start and the Session_End event .

global.asax file:

<%@ Application Language="VB" %>

<script runat="server">

    Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
        ' Code that runs on application startup
        Application("SiteVisitedCounter") = 0
    End Sub
       
    Sub Session_Start(ByVal sender As Object, ByVal e As EventArgs)
        ' Code that runs when a new session is started
           Application.Lock()
        Application("SiteVisitedCounter") = Convert.ToInt32(Application("SiteVisitedCounter")) + 1
        Application.UnLock()
    End Sub
   </script>

HINT:
Convert.ToInt32(string s) method converts the specified string representation of 32-bit signed integer equivalent.

default.aspx file:

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
   
    </div>
    <asp:Button ID="btnClearSesson" runat="server" Text="Clear the session"
        Width="172px" /><br /><br />
    <asp:Label ID="lblsitevisited" runat="server" Text=""></asp:Label>
    </form>
</body>
</html>

default.aspx.vb file:

Partial Class _Default  Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        lblSiteVisited.Text = "No of times site visited = " & Application("SiteVisitedCounter").ToString()
    End Sub

    Protected Sub btnClearSesson_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnClearSesson.Click
        Session.Abandon()
    End Sub
End Class

No comments:

Post a Comment