Click here to Skip to main content
15,892,746 members
Articles / Web Development / ASP.NET
Article

Google Analytics ASP.NET Grid Counter

Rate me:
Please Sign up or sign in to vote.
2.86/5 (7 votes)
7 Apr 2008CPOL4 min read 65.8K   1.2K   36   1
If you need to enumerate the items within a GridView, ListView or any data-bound ASP.NET control the Google Analytics ASP.NET Grid Counter can help you to make it easy.
Visit http://www.sqlnetframework.com/articles/GoogleAnalyticsAspNetGridCounter.aspx to see the original article for a better code syntaxis.
Last update: April 07, 2008
Author: Luis Ramirez.
Author's website: http://www.sqlnetframework.com
Download: Download Google Analytics ASP.NET Grid Counter
Description: Google Analytics ASP.NET Grid Counter: Enumerate the row items in an ASP.NET Grid.

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.

Google Analytics ASP.NET Grid Counter

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.

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.

C#
public class GoogleAnalyticsAspNetGridCounter: Control
{
  protected override void Render(HtmlTextWriter writer)
  {
    writer.Write("1");
  }
}

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.

C#
public class GoogleAnalyticsAspNetGridCounter: Control
{
  protected override void Render(HtmlTextWriter writer)
  {
    IDataItemContainer dataItemContainer = (IDataItemContainer)this.NamingContainer;

    writer.Write(string.Format(this.IndexFormat, dataItemContainer.DataItemIndex + this.IndexOffset));
  }
}

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.

C#
public class GoogleAnalyticsAspNetGridCounter: Control
{
  protected override void Render(HtmlTextWriter writer)
  {
    if (this.NamingContainer == null)
      throw new ApplicationException("The parent naming container cannot be null.");

    if (!(this.NamingContainer is IDataItemContainer))
      throw new ApplicationException("The parent container must implement the IDataItemContainer interface.");

    IDataItemContainer dataItemContainer = (IDataItemContainer)this.NamingContainer;

    writer.Write(string.Format(this.IndexFormat, dataItemContainer.DataItemIndex + this.IndexOffset));
  }
}

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.

XML
<asp:Repeater ID="Repeater1" runat="server" DataSourceID="AccessDataSource1">
    <ItemTemplate>
    <SqlNetFrameworkWebControls:GoogleAnalyticsAspNetGridCounter ID="GoogleAnalyticsAspNetGridCounter1"  
      IndexFormat="{0}. " IndexOffset="1" runat="server"/><%# Eval("FieldName") %><br />
    </ItemTemplate>
</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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) www.sqlnetframework.com
Mexico Mexico
Luis Ramirez is creator and owner of ADO.NET Accelerator. You can use the FREE ADO.NET Accelerator version to reduce more than 50% ADO.NET code from your data access layer. Luis Ramirez is a Microsoft Certified Professional specialized in .NET development. If you want to contact him to work in your projects or for any other inquiry that you have write him to lramirez [at] sqlnetframework [dot] com.

Comments and Discussions

 
GeneralMy vote of 1 Pin
sypatil3-Feb-09 3:21
sypatil3-Feb-09 3:21 

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.