Home   Cover Cover Cover Cover
 

Page Counter

Web Page

/book/solutions/6/Counter.aspx
<%@ Page Language="C#" Inherits="Counter" Src="Counter.aspx.cs" %>
<html>
  <head>
    <title>Hit counter</title>
  </head>
  <body>
    <p>You are visitor number <%=Val()%></p>
  </body>
</html>

Code Behind

/book/solutions/6/Counter.aspx.cs
using System;
using System.Web.UI;

public class Counter : Page {
  
  public int Val() {
    Application.Lock();
    object ctr = Application["hitCounter"];
    int n = ctr == null ? 0 : (int)ctr;
    Application["hitCounter"] = ++n;
    Application.UnLock();
    return n;
  }
}

Try it

Click here.