View Single Post
  #1 (permalink)  
Old November 2nd, 2003, 12:53 AM
shmacgregor shmacgregor is offline
Authorized User
Join Date: Nov 2003
Location: Norco, CA, USA.
Posts: 73
Thanks: 0
Thanked 0 Times in 0 Posts
Default Page Hit Counter

How do I correctly code a Site Hit Counter for the number of times (sessions) a page has been visited.
Reference is Professional ASP.NET 1.0, pages 610 and 611.
In Global.aspx.cs I've inserted the second line with intHitCount
    public class Global : System.Web.HttpApplication {
        protected int intHitCount = 0;

    protected void Session_Start(Object sender, EventArgs e) {
        intHitCount += 1;
        Session["SessionHitCounter"] = intHitCount.ToString();

Then in the Welcome.aspx page file I have:
<form id="Form1" method="post" runat="server">
<h3 class="title"><i>Welcome! You are visitor number</i><br>
<asp:label id="lblHitNo" runat="server" Width="129px" BackColor="#dbdbdd" ForeColor="#ef0063"></asp:label><br>
<i>since November 2003</i></h3>

and in the Welcome.aspx.cs file I have:
public class WelcomeForm : System.Web.UI.Page {
    protected System.Web.UI.WebControls.Label lblHitNo;
    protected System.Web.UI.HtmlControls.HtmlForm Form1;

    private void Page_Load(object sender, System.EventArgs e) {
        lblHitNo.Text = (string)Session["SessionHitCounter"];

and this does NOT work. Strangely, it counts up to about 85 and then starts over.


Reply With Quote