Click here to Skip to main content
5,790,650 members and growing! (22,802 online)
Email Password   helpLost your password?
Web Development » Custom Controls » General     Intermediate

Easy to use Hit Counter

By David S Hobbs

An ASP.NET Hit Counter control that's *very easy* to use.
C#.NET 1.0, Win2K, WinXP, Win2003, Windows, .NET, ASP.NET, Visual Studio, VS.NET2002, Dev

Posted: 20 Jan 2004
Updated: 22 Jan 2004
Views: 140,999
Bookmarked: 50 times
Announcements
Loading...



Search    
Advanced Search
Sitemap
33 votes for this Article.
Popularity: 6.54 Rating: 4.30 out of 5
1 vote, 3.0%
1
0 votes, 0.0%
2
5 votes, 15.2%
3
6 votes, 18.2%
4
21 votes, 63.6%
5

Sample Image - EasyHit.jpg

Introduction

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.

Detail

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)
{    
    Attributes.Add("cellSpacing","1");
    Attributes.Add("cellPadding","1");
    Attributes.Add("border","1");
    Attributes.Add("borderWidth",BorderWidth.ToString());
    Attributes.Add("borderColor",formatColour(BorderColor));

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

    base.RenderEndTag(output);        
}

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.

Notes

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

License

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


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.
Occupation: Web Developer
Location: United Kingdom United Kingdom

Other popular Custom Controls articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 25 of 47 (Total in Forum: 47) (Refresh)FirstPrevNext
QuestionThe directive is missing a 'src' attributememberJFercan17:35 16 Oct '08  
QuestionSeto because when the property value to 10 he does not return that value the counter?memberLuizItatiba9:29 1 Oct '08  
QuestionSessionID QuestionmemberPhantom20813:41 21 Nov '07  
AnswerRe: SessionID QuestionmemberPhantom20819:57 21 Nov '07  
Generalcounter not increasingmemberfyffes23:04 7 Nov '07  
QuestionHyperlink Gridview Hit countermemberchrisyannie18:07 14 Sep '07  
QuestionHit Counter on page with multiview not working?membergsomavia8:47 25 Jan '07  
QuestionVisual Studio 2005member007G16:29 26 Nov '06  
QuestionHow to log image clicks using C#memberhsnmz6:28 6 Oct '06  
Generalhelp about countermemberxecex7:34 23 Jul '05  
GeneralRe: help about countermemberDavid S Hobbs22:31 24 Jul '05  
GeneralCounter not incrementmemberSyerwinLee16:50 12 Jul '05  
GeneralRe: Counter not incrementmemberDavid S Hobbs23:08 12 Jul '05  
GeneralRe: Counter not incrementmemberSyerwinLee21:50 14 Jul '05  
GeneralRe: Counter not incrementmemberravimama1:58 14 Apr '07  
GeneralFix for Counter Reset to Zero bugsussAnonymous7:04 23 Jun '05  
GeneralRe: Fix for Counter Reset to Zero bugsussAnonymous7:05 23 Jun '05  
GeneralCounter IncrementmemberSyed Anis6:17 13 May '05  
GeneralRe: Counter IncrementmemberDavid S Hobbs10:13 15 May '05  
GeneralReset to zero...memberSyerwinlee22:56 2 May '05  
GeneralRe: Reset to zero...memberDavid S Hobbs2:01 3 May '05  
GeneralRe: Reset to zero...memberSyerwinLee1:45 13 Jun '05  
GeneralRe: Reset to zero...memberSyerwinLee17:43 14 Jun '05  
GeneralFantastic but things to considermemberD. Emilio Grimaldo Tuñon22:37 16 Mar '05  
GeneralRe: Fantastic but things to considermemberDavid S Hobbs8:12 17 Mar '05  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 22 Jan 2004
Editor: Smitha Vijayan
Copyright 2004 by David S Hobbs
Everything else Copyright © CodeProject, 1999-2009
Web19 | Advertise on the Code Project