5,692,513 members and growing! (14,595 online)
Email Password   helpLost your password?
Web Development » ASP.NET » General     Intermediate

Custom datagrid pager for SEO (Search Engine Optimization)

By RayBez

A short sample of how to customise the .Net datagrid for SEO (Search Engine Optimization)
VB, .NET, Win2K, WinXP, Win2003, Windows, Visual Studio, ASP.NET, Dev

Posted: 30 Dec 2005
Updated: 15 Feb 2006
Views: 19,556
Bookmarked: 22 times
Announcements
Loading...



Search    
Advanced Search
Sitemap
8 votes for this Article.
Popularity: 3.19 Rating: 3.53 out of 5
2 votes, 25.0%
1
0 votes, 0.0%
2
2 votes, 25.0%
3
1 vote, 12.5%
4
3 votes, 37.5%
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

Sample Image - datagridpager.gif

Introduction

If you use Datagrids on web pages that receive a lot of hits and that only displays information and not to edit the data, then high speed, small size might be what you need.

The standard Datagrid is not optimised for search engine indexing as it uses JavaScript Postback to jump to the next pages.  There is also the slight possibility that your users might not have JavaScript enabled on their browser. We will have a look at implementing a custom Datagrid pager which incorporates a "< Prev" and "Next >" link as well as replacing the Postback buttons with standard anchor tags.

Background

As the majority of internet traffic is generated by search engines, having as many relevant pages spidered and hopefully indexed, is the main concern for most webmasters.  I consider the Datagrid as one of the most powerful controls available to the ASP.Net developer.  It does however have two disadvantages to its use:

  1. It creates a bloated Viewstate which could cause search engines to download unusable data and thereby skipping a page, as some spiders only read the top most part of a web page.
  2. The pager does not create search engine friendly links that are optimised for spiders due to the use of JavaScript Postback.

It might be easier to implement the custom paging than writing custom code yourself, but implementing a custom solution might just be the answer to getting more pages spidered as you will be using anchor tags instead of Postbacks to handle the paging. 

Using the code

There are two aspects to the code:

  1. The customized paging of the Datagrid
  2. The custom paging in the Page_Load event of the form

All the work to customise the Pager is done in the ItemCreated event of the Datagrid.  The main thing to remember is that the Pager is made out of a Label control (the unlinked and therefore current page number) and several DataGridLinkButtons which are the active buttons.  These DataGridLinkButtons are the "culprits" that we want to replace with standard HTML anchor tags.

More descriptive comments are inline.

Datagrid_ItemCreated event

Private Sub CustomGrid_ItemCreated(ByVal sender As Object, ByVal e As _
System.Web.UI.WebControls.DataGridItemEventArgs) _
   Handles CustomGrid.ItemCreated
    If e.Item.ItemType = ListItemType.Pager Then
        Dim Pager As TableCell = CType(e.Item.Controls(0), TableCell)
        Dim myEnumerator As IEnumerator = Pager.Controls.GetEnumerator()

        'Will hold the value of the position of the label

        ' control in the enumerated list of controls

        Dim labelPos As Int16             

        'Flag to indicate that you have reached the label control in the list

        Dim blTag As Boolean = False     
        Dim strObj As String
        Dim litTemp As Literal

        'Counter value to hold the index of the last control in the Pager

        Dim y As Int16             

        resumeHere:
        y = 0
        While (myEnumerator.MoveNext())
            Dim myObject As Object = myEnumerator.Current
            y += 1
            
            'Have we reached the Label control

            If myObject.GetType().Equals(GetType(Label)) Then
                If blTag = False Then
                    labelPos = CType(myObject, Label).Text - 1
                    blTag = True
                End If
                
            'Have we reached DataGridLinkButton control

            ElseIf myObject.GetType.ToString.Equals("System." & _
                "Web.UI.WebControls.DataGridLinkButton") Then

                'Remove the currnt control from the Pager

                Pager.Controls.Remove(myObject)

                'Create a new Literal control with our anchor tag that we 

                'will be using instead of the JavaScript Postback

                litTemp = New Literal        
                
                'Build your custom anchor tag

                litTemp.Text = "<a href=" & chr(34) & "thispage.aspx?page=" & _
                CStr(CInt(CType(myObject, LinkButton).CommandArgument) - 1) _
                & chr(34) & ">" & _
                CType(myObject, LinkButton).CommandArgument & "</a>"

                'Add the new Literal control to the current location in the Datagrid

                Pager.Controls.AddAt(y - 1, litTemp)

                'You have to reload the Enumerator as 

                'we have made changes to the Pager

                myEnumerator = Pager.Controls.GetEnumerator()

                'Loop back and start the enumeration from 

                'the beginning of the Pager

                GoTo resumeHere
            End If
        End While

        'Create the Literal control that will be used to 

        'create the "Next >" button in the pager

        Dim lbNext As New Literal
        
        'Build your custom anchor tag

        lbNext.Text = "<a href=" & chr(34) & "thispage.aspx?page=" & labelPos + 1 _
                  & chr(34) & "> Next &gt;</a>"

        'Create the Literal control that will be used 

        'to create the "< Prev" button in the pager

        Dim lbPrev As New Literal 
        
        'Build your custom anchor tag

        lbPrev.Text = "<a href=" & chr(34) & "thispage.aspx?page=" & _
                      labelPos - 1 & chr(34) & ">&lt; Prev </a>"

        'Get the number of controls in the Pager and 

        'subtract 1 from the number as it is 0 based

        Dim endPagingIndex As Integer = (Pager.Controls.Count - 1)

        'Insert "< Prev" page link if the Datagrid 

        'is on a page other than the first page listed in the Pager

        If (labelPos > 0) Then
            endPagingIndex += 1

            'Ad the control to the Pager at the start 

            'of the Pager which is has an index of 0

            Pager.Controls.AddAt(0, lbPrev)
        End If

        y = CType(sender, DataGrid).PageCount - 1

        'Insert "Next >" page link if the Datagrid is 

        'on a page other than the last page available in the Pager

        If (labelPos < y) Then
            'Ad 1 to the number of controls in the Pager 

            'as we want to add the control to the end of the Pager

            endPagingIndex += 1

            'Ad the control to the Pager at the start 

            'of the Pager which is has an index of "endPagingIndex"

            Pager.Controls.AddAt(endPagingIndex, lbNext)
        End If

        'Additional Pager text added to a Label control

        Dim InfoText As Label = New Label
        InfoText.ID = "it"
        InfoText.Font.Bold = True
        InfoText.Text = "Page: "

        'Ad the new Label as the final step in the process 

        'else you will have to take the new control in the Enumerator

        'into account.  Too much work, just do it at the end!

        'Insert the Label control as the first control of the Pager.

        Pager.Controls.AddAt(0, InfoText)    
    End If
End Sub

The Page_Load event is responsible for catching the page number passed to the form via the page parameter in the querystring. You now have the opportunity to either let the Datagrid do the data paging or you can customise your database querystring to limit the returned results to only include the necessary records. 

If using MySQL as your data engine, you can use the LIMIT function to restrict the number of records returned. e.g. "SELECT * FROM SampleTable WHERE SampleField like ='sampleQuery%' LIMIT " & intP * 25 & ", 25"  If using a similar query, you will only receive 25 records starting at intP * 25.  You can therefore limit the size of the data returned and speed up your page.

Page_Load event

Private Sub Page_Load(ByVal sender As System.Object, _
          ByVal e As System.EventArgs) Handles MyBase.Load
    'The value of the page number will be stored in intP

    Dim intP as Integer 

    'Get the value of the QueryString for the "page" parameter

    If Not Request.QueryString.Item("page") Is Nothing Then
        intP = Request.QueryString.Item("page").ToString.Trim

    'If using custom paging, create a data connection and 

    'pass it the value of intP

    'and do the paging in the database query. In MySQL, 

    'use the LIMIT function to restrict the number of records returned.

    'If not using custom paging, just continue as usual 

    'and let the Datagrid select the relevant records to display.

    CustomGrid.DataSource = YourDataSet

    'Bind the Datagrid to the new Datatable/Dataset

    CustomGrid.DataBind()

    'Set the CurrentPageIndex of the Datagrid manually

    CustomGrid.CurrentPageIndex = intP
End Sub

As paging is now manually handled, you can disable the Viewstate for the Datagrid. Well, that is if you only used the Viewstate so that you could page through the Datagrid. If you still need to be able to edit the grid, then you still need the Viewstate enabled.

By implementing the custom paging, you can achieve three goals:

  1. Disable the Viewstate for the Datagrid and thereby saving bandwidth.
  2. Get more pages indexed by search engines.
  3. Implement custom paging using customised SQL queries and thereby lowering the database time needed to page through the query.

Points of Interest

I hate to use the Goto function while coding, but this is the first time that I did not have an alternative than to use the Goto.  The Pager is not well documented and I had to make due with the limited knowledge that was available. When changes are made to the Enumerator, you have to reload the Enumerator and therefore start from the beginning of the enumerator, therefore the use of the Goto.

This article is based on the articles of Munsifali Rashid and Razi Bin Rais regarding customising the Datagrid pager.

Working sample

This article was born out of necessity as I had to find a way to limit database connection time and get more pages indexed.  You can see a working example of the code on the search page of SearchForMore.Info when you do a search. View the links in the pager at the bottom of the Datagrid.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

RayBez


I am a programmer for a College in London and the author of Search For More Info. I have been involved in a number of web technologies; ASP.Net being my main language of choice.
Occupation: Web Developer
Location: United Kingdom United Kingdom

Other popular ASP.NET 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) 
-- 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: 15 Feb 2006
Editor:
Copyright 2005 by RayBez
Everything else Copyright © CodeProject, 1999-2008
Web10 | Advertise on the Code Project