Google Analytics ASP.NET Grid Counter

Author: Luis Ramirez.
April 7, 2008.

Download GoogleAnalyticsAspNetGridCounter webcontrol

The Google Analytics ASP.NET Grid Counter control will handle the task to enumerate the items within the Google Analytics ASP.NET Grid. See the red zone marked in the Figure 1 to see how The Google Analytics ASP.NET Grid Counter will be used to enumerate the items in the Google Analytics ASP.NET Grid. The Google Analytics ASP.NET Grid Counter can be used by any data-bound control such as: GridView, ListView, etc.


Google Analytics ASP.NET Grid Serie

The Google Analytics ASP.NET Grid Serie will show you how to create an ASP.NET Grid that recreates the appearance and behavior of the Grid displayed in the Google Analytics web site.

In the first part of the Google Analytics ASP.NET Grid Serie we saw How to create a Data Pager for the Google Analytics ASP.NET Grid. In the second part we will see how to create a counter for the Google Analytics ASP.NET Grid.

Google Analytics ASP.NET Grid Counter
Figure 1.

What is the Google Analytics Grid?

Google Analytics is a Google Tool that lets you analyze information about your website's visitors. Google Analytics uses a Grid (see Figure 1) to display the information about your visitors and related issues.

What is the Google Analytics ASP.NET Grid?

The Google Analytics ASP.NET Grid is an ASP.NET control that recreates the behavior and appearance of the Google Analytics Grid.

Google Analytics ASP.NET Grid Serie

The Google Analytics ASP.NET Grid Serie is a set of articles where you will learn how to create the Google Analytics Grid using a set of webcontrols that we are going to create. Each part of the Google Analytics ASP.NET Grid Serie will create a webcontrol that will handle a special task of the Google Analytics Grid. At the end of the Google Analytics ASP.NET Grid Serie you will be able to use the Google Analytics Grid in your ASP.NET projects.

Building the Google Analytics ASP.NET Grid Counter

We are going to create the Google Analytics ASP.NET Grid Counter taking as base class the Control Class. We are going to override the Render method of the Control Class. The overriden Render method will write a number on the client. The written number will be used to enumerate the current item in the Google Analytics ASP.NET Grid.

1public class GoogleAnalyticsAspNetGridCounter: Control 2{ 3 protected override void Render(HtmlTextWriter writer) 4 { 5 writer.Write("1"); 6 } 7}

How to get the index of the current item in the Google Analytics ASP.NET Grid?

The Google Analytics ASP.NET Grid Counter can be used within any data-bound control (e.g. GridView, ListView) that implements the IDataItemContainer Interface. The IDataItemContainer interface will help us to get the index of the current item within the Google Analytics ASP.NET Grid.

1public class GoogleAnalyticsAspNetGridCounter: Control 2{ 3 protected override void Render(HtmlTextWriter writer) 4 { 5 IDataItemContainer dataItemContainer = (IDataItemContainer)this.NamingContainer; 6 7 writer.Write(string.Format(this.IndexFormat, dataItemContainer.DataItemIndex + this.IndexOffset)); 8 } 9}

Implementation of the Google Analytics ASP.NET Grid Counter's Render method

The implementation of the Google Analytics ASP.NET Grid Counter's Render method is shown in the code snippet below. What we do in the Render method is to check whether the NamingContainer of the data-bound control (e.g. GridView, ListView) , where the Google Analytics ASP.NET Grid Counter is used, implements the IDataItemContainer interface. Once that we have checked that the data-bound control (e.g. ListView, GridView) implements the IDataItemContainer we use the DataItemIndex property of the IDataItemContainer interface to get the index of the current item. You can download the full implementation of the Google Analytics ASP.NET Grid Counter to use it in your ASP.NET web projects.

1public class GoogleAnalyticsAspNetGridCounter: Control 2{ 3 protected override void Render(HtmlTextWriter writer) 4 { 5 if (this.NamingContainer == null) 6 throw new ApplicationException("The parent naming container cannot be null."); 7 8 if (!(this.NamingContainer is IDataItemContainer)) 9 throw new ApplicationException("The parent container must implement the IDataItemContainer interface."); 10 11 IDataItemContainer dataItemContainer = (IDataItemContainer)this.NamingContainer; 12 13 writer.Write(string.Format(this.IndexFormat, dataItemContainer.DataItemIndex + this.IndexOffset)); 14 } 15}

How to use the Google Analytics ASP.NET Grid Counter?

We are going to use the Google Analytics ASP.NET Grid Counter within a Repeater ASP.NET control to show how to use the Google Analytics ASP.NET Grid Counter.

1<asp:Repeater ID="Repeater1" runat="server" DataSourceID="AccessDataSource1"> 2 <ItemTemplate> 3 <SqlNetFrameworkWebControls:GoogleAnalyticsAspNetGridCounter ID="GoogleAnalyticsAspNetGridCounter1" 4 IndexFormat="{0}. " IndexOffset="1" runat="server"/><%# Eval("FieldName") %><br /> 5 </ItemTemplate> 6</asp:Repeater>

Conclusion

Google Analytics ASP.NET Grid Counter used in a Repeater
Figure 2.

Now we can use the Google Analytics ASP.NET Grid Counter to enumerate the items within the Google Analytics ASP.NET Grid. Moreover we can use the Google Analytics ASP.NET Grid Counter to enumerate the items within any data-bound control that implements the IDataItemContainer Interface.