Click here to Skip to main content
13,093,720 members (61,847 online)
Click here to Skip to main content
Add your own
alternative version


63 bookmarked
Posted 20 Jan 2004

Easy to use Hit Counter

, 22 Jan 2004
Rate this:
Please Sign up or sign in to vote.
An ASP.NET Hit Counter control that's *very easy* to use.

Sample Image - EasyHit.jpg


I wanted to create a hit counter for my ASP.NET applications, but I wanted it to be very easy to deploy. I did not want to have to upload a set of images, set up database access or anything like that. I wanted something that I could add to the Visual Studio toolbox and then simply drag and drop it onto my pages.

The Solution

I have designed a custom control that renders an HTML table with each cell being one of the digits of the counter. The style of the counter is totally configurable from within the Visual Studio designer. The picture above shows an example of the counter in action.

The number of hits is stored in a text file that will be automatically created when the page is hit for the first time. You can choose to increment the value stored in that file each time the page is hit, or you can choose a delay and in that case the file will only be updated at the interval you have specified.

Because the value is stored in a text file, if you update your application, the page count is not lost. If there was no text file and we only stored the counter as an Application variable, you would reset the counter to zero when you updated your site.

Here are the most important properties of the control:

  • Visible

    Determines if the counter is shown on the page or not. If the value is True (the default) then the user can see the counter on the page. If the value is set to False then the counter is incremented but the counter is not displayed on the page.

  • Padding

    The minimum number of digits to show, it defaults to 5 so that 3 hits displays as 00003.

  • WriteDelay

    The interval at which the text file is updated. In the Visual Studio designer, this is in the format hh:mm:ss. If you specify a zero value (the default, i.e. 00:00:00) then the text file is updated each time the page is accessed.

  • TextFileName

    The file where the hit count is stored on the server, relative to the application root. If you want to put this file in a sub-folder, you can do so (e.g. bin\count.txt would store the file in the application bin folder instead of the root).

All of the other properties are just for applying colors and formatting and they all should work in the normal way.


Rendering the control is surprisingly simple. Firstly, I have overridden the TagKey property of the WebControl to tell it to send a table tag rather than the default span tag.

Next, I add the table attributes, like borders and spacing and then send the start tag to the browser. A foreach loop adds each column to the table with the digit included. Finally, the end tag is sent and we’re done.

protected override void Render(HtmlTextWriter output)

    foreach (char c in mCount.ToString().PadLeft(mPadding,'0'))
        output.Write("<TD align=\"middle\">"+c+"</TD>");


The counter is incremented in the overridden OnLoad method. This code first checks to see if the Context is null. This tells us if the control is being shown in the Visual Studio IDE or not. I'm also checking to see if the page is a postback. In either case, the counter is not incremented. Otherwise the hit counter is increased by one.

If you want to use this control, download the source code and add a reference to DSHHitCounter.dll to your ASP.NET application. If you add the HitCounter control to your toolbox, you can then simply drag and drop the control onto an ASPX page in the designer and immediately run it. It doesn’t get much easier than that.


This control requires that the application has FileIOPermissionAccess.Write, FileIOPermissionAccess.Append and FileIOPermissionAccess.Read rights to the folder hosting it.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

David S Hobbs
Web Developer
United Kingdom United Kingdom
I have been developing software on various platforms for over 14 years. For my sins I recently spent two years managing a development team at some large Redmond-based software company....

I'm now enjoying working as a freelance software developer from home, although I seem to be working harder than ever. My main area of work is ASP.Net development with C#.

I like to take as many holidays as possible, but always end up taking a book on software development with me. I'm not happy unless I'm learning something new.

You may also be interested in...


Comments and Discussions

QuestionWorks but Pin
vipz0076-Oct-13 20:33
membervipz0076-Oct-13 20:33 
QuestionWorks Pin
lukasz44-Jan-13 6:53
memberlukasz44-Jan-13 6:53 
Questionhow to create hit counter using image in c# Pin
shalinibharani20-Jul-12 1:19
membershalinibharani20-Jul-12 1:19 
QuestionHow to apply it in different pages and different count Pin
geminigentle18-Mar-12 7:47
membergeminigentle18-Mar-12 7:47 
QuestionRegarding Hit Counter Pin
ritesh27128-Dec-09 0:42
memberritesh27128-Dec-09 0:42 
AnswerRe: Regarding Hit Counter Pin
Member 1021468028-Jan-14 22:04
professionalMember 1021468028-Jan-14 22:04 
QuestionThe directive is missing a 'src' attribute Pin
JFercan16-Oct-08 16:35
memberJFercan16-Oct-08 16:35 
QuestionSeto because when the property value to 10 he does not return that value the counter? Pin
LuizItatiba1-Oct-08 8:29
memberLuizItatiba1-Oct-08 8:29 
QuestionSessionID Question Pin
Phantom20821-Nov-07 12:41
memberPhantom20821-Nov-07 12:41 
AnswerRe: SessionID Question Pin
Phantom20821-Nov-07 18:57
memberPhantom20821-Nov-07 18:57 
Generalcounter not increasing Pin
fyffes7-Nov-07 22:04
memberfyffes7-Nov-07 22:04 
QuestionHyperlink Gridview Hit counter Pin
chrisyannie14-Sep-07 17:07
memberchrisyannie14-Sep-07 17:07 
QuestionHit Counter on page with multiview not working? Pin
gsomavia25-Jan-07 7:47
membergsomavia25-Jan-07 7:47 
QuestionVisual Studio 2005 Pin
007G26-Nov-06 15:29
member007G26-Nov-06 15:29 
QuestionHow to log image clicks using C# Pin
hsnmz6-Oct-06 5:28
memberhsnmz6-Oct-06 5:28 
Generalhelp about counter Pin
xecex23-Jul-05 6:34
memberxecex23-Jul-05 6:34 
GeneralRe: help about counter Pin
David S Hobbs24-Jul-05 21:31
memberDavid S Hobbs24-Jul-05 21:31 
GeneralCounter not increment Pin
SyerwinLee12-Jul-05 15:50
memberSyerwinLee12-Jul-05 15:50 
GeneralRe: Counter not increment Pin
David S Hobbs12-Jul-05 22:08
memberDavid S Hobbs12-Jul-05 22:08 
GeneralRe: Counter not increment Pin
SyerwinLee14-Jul-05 20:50
memberSyerwinLee14-Jul-05 20:50 
GeneralRe: Counter not increment Pin
ravimama14-Apr-07 0:58
memberravimama14-Apr-07 0:58 
GeneralFix for Counter Reset to Zero bug Pin
Anonymous23-Jun-05 6:04
sussAnonymous23-Jun-05 6:04 
GeneralRe: Fix for Counter Reset to Zero bug Pin
Anonymous23-Jun-05 6:05
sussAnonymous23-Jun-05 6:05 
GeneralCounter Increment Pin
Syed Anis13-May-05 5:17
memberSyed Anis13-May-05 5:17 
GeneralRe: Counter Increment Pin
David S Hobbs15-May-05 9:13
memberDavid S Hobbs15-May-05 9:13 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.170813.1 | Last Updated 23 Jan 2004
Article Copyright 2004 by David S Hobbs
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid