ASP.NET DataGrid - Custom Paging using the Cache Object - Part I

ASP.NET DataGrid is one of the coolest controls (well its quite late now to remark, but I cannot stop from starting like this) that has been used by almost everyone who does coding in ASP.NET. Though there are other controls like Repeater, DataList, the popularity DataGrid has got among developers is enviable.

One of the primary concerns when using DataGrid web server control is the Paging functionality. In real world scenario you will always end up with huge data that cannot be showed once for all in a single page. The DataGrid needs to implement paging by which you show only limited number of records and provide navigation links for seeing the next set of records, previous records, etc., However, there are certain limitations in using the built-in paging functionality. They are


1. The Pager Style mode is limited to "Next Previous" or Numeric "1,2,.." etc., whereas in certain places we would like to have custom text which says 1-5 of 10 records, First page, Last Page in addition to the previous, next page etc.,

2. Everytime, the page link is clicked, the entire set of records is going to be retreived though only the actual number of items will be shown. Assume you have 10,000 records. Everytime the 10,000 records will be retrieved when you navigate between the pager links. This can be heavy over the network.

3. You need to bind the DataGrid everytime on the PageIndexChanged event.

While the above are certain performance overheads, still, for basic paging requirement, the built-in paging mechanism is more than enough. However, if you care for performance and want to implement your own way of efficient paging, DataGrid still offers you the way to do it. It welcomes you to implement custom paging for which you have to write the code. In built-in paging, it does the plumbing work internally whereas, in custom paging, you need to dirty your hands in code.

There are several ways in which you can implement custom paging. There have been wealth of articles on the same ever since ASP.NET 1.0 got released and I am sure there must be some articles already on the topic I am writing now. But this is just another approach to help people who want to implement custom paging and expand the possibilities with DataGrid.

Having said that, lets start our first step in implementing Custom Paging.

1. Declare the DataGrid with AllowPaging="true"

<ASP:DATAGRID id="DataGrid1" Runat="server" AutoGenerateColumns="true" PageSize="5" AllowPaging="true" PagerStyle-Visible="False">
</ASP:DATAGRID>


If you notice in the above declaration, we have set AllowPaging="true" which is normal in any case where you implement paging. Secondly, we have set the PagerStyle-Visible="false" to ensure that DataGrid doesnt show up its default pager style "< >" since we have set the allowpaging to true.

2. The next step is declaring a Panel which contains the Pager links

&lt;asp:Panel id="Panel1" Runat="server" Visible="False">

&lt;TR>

&lt;TD id="LinkButtons" colSpan="2" runat="server">

&lt;ASP:LINKBUTTON id="FirstPage" Runat="server" text="First" OnCommand="NavigationLink_Click" CommandName="First">&lt;/ASP:LINKBUTTON>

&lt;ASP:LINKBUTTON id="PreviousPage" Runat="server" text="Previous" OnCommand="NavigationLink_Click" CommandName="Prev">&lt;/ASP:LINKBUTTON>

&lt;ASP:LABEL id="lblPageDetails" runat="Server" >&lt;/ASP:LABEL>

&lt;ASP:LINKBUTTON id="NextPage" Runat="server" text="Next" OnCommand="NavigationLink_Click" CommandName="Next">&lt;/ASP:LINKBUTTON>

&lt;ASP:LINKBUTTON id="LastPage" Runat="server" text="Last" OnCommand="NavigationLink_Click" CommandName="Last">&lt;/ASP:LINKBUTTON>

&lt;/TD>

&lt;/TR>

&lt;/asp:Panel>

In the above code you can notice that we have 4 linkbuttons one each for "First, Last, Previous, Next". All of them link to a single method "NavigationLink_Click", but pass different CommandNames such as "First", "Previous", "Next", "Last" to specify which button is clicked.

Also, we have a panel covering these link buttons. We will explore on how to implement the NavigationLink_Click method in the next part of this article.

Cheers and Happy Programming !!!

Print | posted on Wednesday, December 28, 2005 4:04 AM

Comments on this post

# re: ASP.NET DataGrid - Custom Paging using the Cache Object - Part I

Requesting Gravatar...
Kindly request you to plz send me the next part of this article as early as possible...
Left by Manoj Jagtap on Dec 15, 2006 12:12 PM

# re: ASP.NET DataGrid - Custom Paging using the Cache Object - Part I

Requesting Gravatar...
Kindly request you to plz send me the next part of this article as early as possible...

manoj.jagtap@gmail.com
Left by Manoj Jagtap on Dec 15, 2006 12:13 PM

# re: ASP.NET DataGrid - Custom Paging using the Cache Object - Part I

Requesting Gravatar...
I request you send me still more expalnation
Left by Saravanan on Apr 29, 2008 9:37 PM

# re: ASP.NET DataGrid - Custom Paging using the Cache Object - Part I

Requesting Gravatar...
This article is very useful for me and suits my requirements.
requesting you please send me the next part of this article as soon as possible
Left by joti on Sep 20, 2008 2:01 PM

# re: ASP.NET DataGrid - Custom Paging using the Cache Object - Part I

Requesting Gravatar...
Waste Article.
I hate these coders
Go & learn some coding.
Left by Roger on Feb 23, 2009 5:51 PM

# re: ASP.NET DataGrid - Custom Paging using the Cache Object - Part I

Requesting Gravatar...
you please send me the next part of this article as soon as possible
Left by mario oyunları on Sep 22, 2009 12:14 AM

# re: ASP.NET DataGrid - Custom Paging using the Cache Object - Part I

Requesting Gravatar...
I request you send me still more expalnation...
Left by mario oyunları on Jan 04, 2010 3:05 AM

# re: ASP.NET DataGrid - Custom Paging using the Cache Object - Part I

Requesting Gravatar...
Waste Article.I hate these coders, thanks for post
Left by en güzel oyunlar on Jan 22, 2010 6:59 AM

# re: ASP.NET DataGrid - Custom Paging using the Cache Object - Part I

Requesting Gravatar...
Suicide is not the answer to depression, although I do admit that a person in depression feels very down and lonely but still it’s not an easy way out. And I am sure not many people would be opting for it as the economy begins to recover slowly.
Left by eğlenceli oyunlar on Feb 24, 2010 5:21 AM

Your comment:

 (will show your gravatar)