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.
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:
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.
The minimum number of digits to show, it defaults to 5 so that 3 hits displays as 00003.
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.
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
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'))
The counter is incremented in the overridden
OnLoad method. This code first checks to see if the
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.Read rights to the folder hosting it.