How to create Hit Counter Usercontrol in ASP.NET

No.of Views1134
Bookmarked0 times
Downloads 
Votes0
By  Bharat   On  17 Nov 2010 05:11:08
Tag : ASP.NET , How to
In this codesnippet, i will show you, How to create Hit Counter Usercontrol in ASP.NET.
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at info@codegain.com

 

Introduction

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.

Implementation

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)
    {this.countMe();

        DataSet tmpDs = new DataSet();
        tmpDs.ReadXml(Server.MapPath("~/counter.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"));

    }
}

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,

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

 That's all for implementation of the hit counter.

How to use

Just drag and drop the this user control, where you want to capture the visitors count.

Conclusion

In this codesnippet, i had demonstrated to you, How to create Hit Counter UserControl in ASP.NET.

 
Sign Up to vote for this article
 
About Author
 
Bharat
Occupation-Software Engineer
Company-Espranza
Member Type-Junior
Location-India
Joined date-13 Nov 2010
Home Page-
Blog Page-http://usingaspdotnet.blogspot.com
 
 
Other popularSectionarticles
    Auto Growing TextBox or TextArea in ASP.NET
    Published Date : 08/May/2010
    In this code snippet, you will learn how to bind DropdownList within the ListView in ASP.NET. The ListView is powerful control and fully customizable using templates.
    Published Date : 10/Oct/2010
    In this codesnippet, i will show How to Delete Row in GridView using JQuery in ASP.NET.
    Published Date : 20/Jul/2011
    In this snippet, I will show how to format a cell and apply style in gridview using JQuery. Sometimes we may need to apply the format for a particular cell based on the cell value; it can be done in within DataRowBound event in asp.net.
    Published Date : 05/Jan/2011
    In this snippet I will explain how to add controls dynamically in asp.net and register events for the controls and make it work events perfectly. Last week I have read the forums many readers asking about add controls dynamically in asp.net giving problems and also it not working properly with events
    Published Date : 03/Jan/2011
Comments
There is no comments for this articles.
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top