Pages

Subscribe:

Ads 468x60px

Monday, May 9, 2011

User Counter

just copy and past the code, this snipt will help you to count visits on your website, no need to build any database it will use an xml file light weight code using c#.NET. Code for .aspx.cs file.

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 UC_UserHIT_Counter : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        this.countMe();

        DataSet tmpDs = new DataSet();
        tmpDs.ReadXml(Server.MapPath("~/counter.xml"));
        lbl_Counter.Text = "<b><font color='blue'>Total Visits:</font></b> <font color='green'>" + tmpDs.Tables[0].Rows[0]["hits"].ToString() + "</font> ";
    }
    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"));
    }
}
Code for .aspx file.
just put it any where in the aspx file.
<asp:Label ID="lbl_Counter" runat="server"></asp:Label>

No comments:

Post a Comment