5,446,542 members and growing! (19,417 online)
Email Password   helpLost your password?
Web Development » ASP.NET Controls » Grid Controls     Beginner License: The Code Project Open License (CPOL)

Google Analytics ASP.NET Grid Counter

By Luis Ramirez

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.
C# (C# 2.0, C#), .NET (.NET, .NET 2.0), ASP.NET, Dev

Posted: 7 Apr 2008
Updated: 7 Apr 2008
Views: 6,007
Bookmarked: 12 times
Announcements
Want a new Job?



Search    
Advanced Search
Sitemap
3 votes for this Article.
Popularity: 1.31 Rating: 2.75 out of 5
1 vote, 33.3%
1
1 vote, 33.3%
2
0 votes, 0.0%
3
1 vote, 33.3%
4
0 votes, 0.0%
5
Note: This is an unedited contribution. If this article is inappropriate, needs attention or copies someone else's work without reference then please Report This Article
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.

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.

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.

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.

<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)

About the Author

Luis Ramirez


Luis Ramirez is creator and owner of http://www.sqlnetframework.com. The SqlNetFramework is a compelling alternative to the Microsoft Data Access Application Block. 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.com.
Occupation: Software Developer (Senior)
Company: www.sqlnetframework.com
Location: Mexico Mexico

Other popular ASP.NET 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   
  (Refresh) 
Subject  Author Date 
-- There are no messages in this forum --

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

PermaLink | Privacy | Terms of Use
Last Updated: 7 Apr 2008
Editor:
Copyright 2008 by Luis Ramirez
Everything else Copyright © CodeProject, 1999-2008
Web13 | Advertise on the Code Project