An ASP.NET Blog
I work for Microsoft and help people and businesses make better use of technolgy to realize their full potential. The opinions mentioned herein are solely mine and do not reflect those of my employer.

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

Wednesday, December 28, 2005 4:04 AM

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


Feedback

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

Kindly request you to plz send me the next part of this article as early as possible... 12/15/2006 12:12 PM | Manoj Jagtap

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

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

manoj.jagtap@gmail.com 12/15/2006 12:13 PM | Manoj Jagtap

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

sedfertrert 3/2/2008 2:54 PM | lalala

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

I request you send me still more expalnation 4/29/2008 9:37 PM | Saravanan

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

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 9/20/2008 2:01 PM | joti

Post a comment





 

Please add 2 and 3 and type the answer here: