How to create Hit Counter Usercontrol in ASP.NET RRS feed

  • Question

  • User-414050457 posted


    In this codesnippet, i will show you, How to create Hit Counter UserControl in ASP.NET.The hit counter is useful, when you are develop a web page, and then you have to monitor how many visitors visiting your page.


    Create a usercontrol using visual studio, and then add a label control to display count as like following,

    <asp:Label ID="lblCounter" runat="server"></asp:Label>

    Next ,add server side code for capture the visitor count.

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

    public partial class WebUserControl : System.Web.UI.UserControl
    {protected void Page_Load(object sender, EventArgs e)

    DataSet tmpDs = new DataSet();
    lblCounter.Text = tmpDs.Tables[0].Rows[0]["hits"].ToString();
    }private void countMe()

    DataSet tmpDs = new DataSet();
    tmpDs.ReadXml(Server.MapPath("~/counter.xml"));int hits = Int32.Parse(tmpDs.Tables[0].Rows[0]["hits"].ToString());

    hits += 1;

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



    The important point,here you have to place xml file named counter.xml in your web server location.The XML file stricture will be following,

    Saturday, November 20, 2010 2:14 AM

All replies

  • User759660354 posted

    Interesting but how you deal with ohter interesting features like.

    • Average time on site
    • Vistied pages
    • Bounce rate
    • Unique visitors

    Maybe you should look at google analytics.


    Saturday, November 20, 2010 2:24 AM
  • User420802475 posted

    This type of control is especially found in blogs. There is difference between hits and clicks, if someone create control in asp.net for which highlight hits as well as clicks for each post of a particular blog, then it will be greatly appreciated.


    Tuesday, November 23, 2010 4:34 AM