Hit Counter in ASP.Net

Place webuser control 

C# code for webuser control
 

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)

    {

        this.countMe();

        DataSet tmpDs = new DataSet();

        tmpDs.ReadXml(Server.MapPath("~/hitcounter.xml"));

        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();

        tmpDs.WriteXml(Server.MapPath("~/counter.xml"));

    }

}

Html code for webuser control 


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


Place xmlfile named hitcounter.xml 


Paste this code in xml file 


<counter>
<count>
<hits>0</hits>
</count>
</counter>

Drag & drop webuser contol in which page use want to add counter.

Ebook Download
View all
Learn
View all