Controls Supporting the Site Counters Service
The controls listed in Table 1 support the Site Counters service:
List of controls supporting the Site Counters service
Displays an advertisement banner on a Web page
A control that displays a link to another Web page
Displays a push button control on the Web page
A control that displays an image and responds to mouse clicks on the image
Displays a hyperlink style button control on a Web page
Represents a link (designed for phone devices)
Displays a clickable image map
Even though the ImageButton
control supports the Site Counters service, it does not work in Visual Studio 2005 Beta 1. I will take this opportunity to show you a workaround for this bug; the solution will illustrate not only how to make ImageButton
work with the service but also show you how to use the SiteCounter class to manually record clicks for any control that isn't supported.
In Figure 7
I've modified the default.aspx page to include four ImageButton
controls that replace the four Hyperlink
controls I used earlier.
|Figure 7. Default.aspx now uses four ImageButton controls in place of the four Hyperlink controls. |
The following is the source view of the ImageButton
<asp:ImageButton ID="ImageButton1" Runat="server"
<asp:ImageButton ID="ImageButton2" Runat="server"
<asp:ImageButton ID="ImageButton3" Runat="server"
<asp:ImageButton ID="ImageButton4" Runat="server"
Notice that each control is linked to a redirect.aspx
page via the PostBackUrl
property. A query string is also passed to the redirect.aspx
page. This query string contains the destination page for each ImageButton
Here is the redirect.aspx
page (which I have added to my project):
Sub Page_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) _
Dim URL As String = Request.QueryString("URL")
If URL IsNot Nothing Then
SiteCounters.Write("Links (manual)", URL, _
"Click", URL, True, _
True, Nothing, 1)
In the Page_Load
event of the redirect.aspx
page, I extract the query string using the Request.QueryString
method. Then, I write the Site Counter entries to the database using the Write
method of the SiteCounters class. The Write
method is overloaded and allows you to specify information such as Counter group
, Counter name
, Counter event
, and Navigate URL
I use the FlushAll
method in the redirect.aspx
page to write the Site Counter information immediately to the database. The Site Counter service normally caches the data before writing it to the database at regular intervals. After you've used the FlushAll
method, redirect the user to the destination page by using the Response.Redirect
With that done, I can now track the clicks of the ImageButton
Viewing Site Statistics
Apart from viewing the site statistics directly from the database, a much more practical approach would be to programmatically retrieve the values from the database. The Site Counters service supports several methods to allow you to retrieve site statistics from the database (see Figure 8
To illustrate how to retrieve the site statistics, I have added a Button control and a GridView control to the page (see Figure 9
Figure 8. Use the SiteCounters class to manually get site traffic statistics from the database.
Figure 9. Add a Button and a GridView control to the page.
In the code-behind of the Get Site Counter Stats
button, code the following:
Sub btnGetStats_Click(ByVal sender As Object, _
ByVal e As System.EventArgs)
Dim ds As DataSet
ds = SiteCounters.GetRows(DateTime.MinValue, _
DateTime.MaxValue, Nothing, Nothing)
GridView1.DataSource = ds
|Figure 10. The site statistics, culled from the Site Counters class, are shown in a GridView control.|
The statistics retrieved are returned as a Dataset object, which is then bound to a GridView control (see Figure 10
The Site Counters service has really simplified the tasks of tracking page usage. No longer do you need to write utilities to extract information from the log files; everything can be done automatically within ASP.NET. Best of all, the Site Counters service works natively with the many Web server controls, in particular the AdRotator
control, which will support your efforts to make your site a commercial success.