ASP.NET 2.0 DataGrid Custom Paging[by Ted Heng]

ASP.NET 2.0 DataGrid Custom Paging

Author: Ted Heng, Senior Consultant, Premier Partner ISV, US Enterprise Services, Microsoft Corporation

June 30, 2010

 

Contents

Introduction. 1

Technique. 1

Screenshots. 2

Source Code. 2

Conclusion. 6

 

 

Introduction

In this blog, I will demonstrate a technique where you can customize your paging behavior in combination with the standard DataGrid control’s NumericPage paging feature.  The standard paging modes for the DataGrid are NumericPages and PrevNext.  You can enable AllowCustomPaging to do a more complex paging customization, but that requires you to manipulate the VirtualItemCount and others which isn’t always necessary for many paging customization needs.  So, this is the simplest way that I know how to accomplish custom paging.

 

Technique

The DataGrid creates two paging rows one at the top of the table if the option is to display the paging navigation there and/or another one at the bottom of the table if the option is to display there.  If you don’t use the auto-generated columns, the paging table cell spans across the entire table and there is a Controls list that has the numeric page links.  This technique for adding custom paging behaviors to the DataGrid control requires manipulating this Controls list.  Here are the main steps that you need to do.

1.      Add the Init event handler for the DataGrid control – in this Init event handler, create the additional navigation controls, such as, ImageButton or LinkButton, to be added to the paging Controls list.  If you would like to have the paging behavior to include First Page, Previous Page, Numeric Pages, Next Page, and Last Page, you will need to add First Page, Previous Page, Next Page, and Last Page link controls to the Controls list.

2.      For each additional navigation link, create an event handler for the Click event.  This allows your program to handle the event when the user clicks on the link.

3.      Add the ItemCreated event handler for the DataGrid control – you will add the additional navigation links to the Controls list in this event handler.

Screenshots

Using the AdventureWorksLT2008, here is the list from the Customer table.

Source Code

Here is the entire source code for the default.aspx.

Here is the entire source code for default.aspx.cs.

Conclusion

This technique shows you how to customize your navigation behaviors with very simple steps and still leveraging the built-in paging feature of the DataGrid control.  I hope that this is useful for your application.

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章