How To Store Page Hit Count In ASP.NET

 In ASPX Page....

While developing a static website, where no database is used then you might need to add a functionality of page hit count in your website. Its very easy and simple to achieve this functionality using a xml file. we can dynamically update the xml file data and can show this to your users.

Here I am showing you some of my codes I have done for achieving this functionality in a static website.

Add the following code in your .aspx page, where you want the hit count to appear. Just add the label there with an id.

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm2.aspx.cs" 
Inherits="Send_SMS.WebForm2" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
<html xmlns="">
<head runat="server">
    <form id="form1" runat="server">
      <asp:Label ID="lblCounter" runat="server"></asp:Label>

      Now add a xml file named "counter.xml " to the root folder of your project. Here is how your xml file will look like.

<?xml version="1.0" standalone="yes"?>

In Code Behind Page ....

Just add the bellow written codes into the .cs file of the aspx page you have added. Here we are storing the hit count in a XML file and update the data of the XML file dynamically.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace Send_SMS
    public partial class WebForm2 : System.Web.UI.Page
        protected void Page_Load(object sender, EventArgs e)

            DataSet tmpDs = new DataSet();

            tmpDs.ReadXml("C:/Users/tapank/Documents/Send SMS/counter.xml");

            lblCounter.Text = tmpDs.Tables[0].Rows[0]["hits"].ToString();

        private void countMe()
            DataSet tmpDs = new DataSet();

            tmpDs.ReadXml("C:/Users/tapank/Documents/Send SMS/counter.xml");

            int hits = Int32.Parse(tmpDs.Tables[0].Rows[0]["hits"].ToString());

            hits += 1;

            tmpDs.Tables[0].Rows[0]["hits"] = hits.ToString();

            tmpDs.WriteXml("C:/Users/tapank/Documents/Send SMS/counter.xml");


Basically what we did in this article is something like, we are storing the hit count of a page in a xml file and updating that data dynamically. This is very helpful if you are not using any database in your website basically for static websites.

Happy coding.....


  1. Hi Tapan,
    Sorry this comment is not anyway related to your post.
    I just wanted to know the whereabouts of Pandit Sir. (Pandit Benudhar Parida). He was my sanskrit teacher in Sunabeda, Odisha. I need to see him. Please mail me if you have any information about him.
    My mail ID:
    [email protected]
    [email protected]

    God Bless You Dude..

  2. hallo,
    this code is working well, but the problem is that while we refresh the page then also it is increasing hit count value... i had also used !IsPostBack bt still doesn't work..

    my email:
    [email protected]

    1. Hi Pratik,

      Instead of using the !ispostback in page load, the code block inside the !isPostback gets executed that's the problem why its is increasing it once when you refresh the page, but try reloading the page again, you will see the code inside the !isPostback will not be executed.

      Tapan kumar

  3. Hi Tapan,
    Thanks for Posting....It saves me today....