posted on Friday, October 26, 2007 2:01 AM by thomasswilliams

A Better Pager for the ASP.NET 2.0 GridView

The default pager on the ASP.NET 2.0 GridView is not pretty, and is barely practical. I've tried styling it and mucking around with the RowDataBound and RowCreated events behind the scenes, and finally I thought I'd just have to learn to live with its limitations.

This week I discovered some great pager code on CodeProject by Daniel Vaughan, and below I've shown the difference that the "Amazon-esque Pager" control made for me:

Daniel even took the time to answer my questions on getting his C# code working with databound gridviews. Brilliant!

If you're frustrated by the limits of the in-built Pager, give Daniel's code a try and improve the situation for your users at the same time.

Tags: pager, asp.net, gridview, amazon, codeproject

Comments

# Interesting Finds: October 26, 2007 @ Friday, October 26, 2007 11:28 AM

Anonymous

# re: A Better Pager for the ASP.NET 2.0 GridView @ Wednesday, November 28, 2007 4:52 AM

Hi Thomas,

I was wondering if you could add some code or maybe just some words of wisdom on how you connected the pager to a databound gridview.

In my project I have a GridView which uses an ObjectDataSource to fetch the row data, and the standard pager works fine with this. How can I 'replace' this pager with this "new and improved" version? I Daniels code on CodeProject I can see he uses a Repeater mostly, and codes a lot of the stuff in the Page code behind file, whereas I have my "fetch data" code stuffed away in my Adapter (ObjectDataSource).

Any help or guidance would be greatly appreciated!

/Nicolai

Nicolai

# re: A Better Pager for the ASP.NET 2.0 GridView @ Wednesday, November 28, 2007 6:44 PM

G'day Nicolai - I asked exactly the same question, and Daniel provided some helpful code in one of the comments on his article at http://www.codeproject.com/useritems/PagerControl.asp?msg=2281920#xx2281920xx

If you have your GridView working, adding Daniel's pager is fairly straightforward - you'll need to get an item count in your Page_Load *before* the grid loads though, I haven't found a workaround for this.

Let me know how you go!

Cheers,

Thomas

thomasswilliams

# re: A Better Pager for the ASP.NET 2.0 GridView @ Thursday, November 29, 2007 12:09 PM

Hi Thomas,

Thank you very much for pointing that out. I guess I ran over it a little fast. Very easy indeed! It now works "perfectly" and I've added my own touch by customizing the background colors and the css style in order to make it match the color scheme used in my project!

The reason I put perfectly in quotes is because I still have one problem. It might just be because I'm overlooking something easy, but hopefully you can help me out again :-)

My GridView is one where the user has the possibility of editing and deleting rows if he/she desires. This poses a problem because the OnPageChanging event is not fired when the users decides to do so, and this is most of the time ok, however in some cases (the extremes) it is not ok. Consider this:

I have a PageSize of 2 and a total of 3 Rows. This gives me 2 pages: One page (the first) with 2 Rows displayed, and another page (the second) with 1 Row displayed. If I browse to the second page and decide to delete the Row there, I will be stuck on page 2 even though I have deleted the row from the underlying list of data. This also has the effect that my pager still displays 'showing 3 to 3 of 3' although my Adapter only has 2 Rows in its underlying list of data.

Is this because I should catch this in my page code behind somewhere (only handling IsPostBack==true) and then sort of synchronize the GridView and the Pager again? I hope you understand my problem.

Thank you again :-)

Nicolai

# re: A Better Pager for the ASP.NET 2.0 GridView @ Friday, November 30, 2007 12:43 AM

G'day again Nicolai - I'd be interested to see what a GridView with paging on did by default in that situation. Hmmm...

Thomas

thomasswilliams

# re: A Better Pager for the ASP.NET 2.0 GridView @ Friday, November 30, 2007 3:14 AM

Hi Thomas,

I simple test revealed that the default paging does not handle this any better. In fact it is even worse. It deletes the row nicely enough (which of course has nothing to do with the pager) but afterwards it simply does not display the gridview at all?! I guess this is because it tries to show the second page where nothing is present, but thats just a guess.

I will try to mess around with the postback requests to see if I can synchronize the improved pager with the gridview, it must be possible! I will post again if/when I find (or not find) an answer.

Nicolai

# re: A Better Pager for the ASP.NET 2.0 GridView @ Monday, December 10, 2007 5:12 PM

Hi Thomas,

I have been quite busy lately so I'm sorry I haven't written back before now. I did figure out how to solve the problem though, and I've posted my solution on codeproject as a comment to Daniels posts.

http://www.codeproject.com/KB/aspnet/PagerControl.aspx?fid=473845&noise=3&df=90&mpp=25&sort=Position&view=Quick&select=2352857#xx2352857xx

Thank you for your help and guidance!!

Regards

Nicolai

# re: A Better Pager for the ASP.NET 2.0 GridView @ Wednesday, December 12, 2007 4:07 PM

Well done Nicolai - I read your comment on CodeProject and am impressed with the simple and elegant solution, that doesn't impact on teh existing "Amazon Pager" code.

And your solution works perfectly for you - a bigger bonus!

Cheers, Thomas

thomasswilliams

# OT: Merry Christmas and Blog Stats 2007 Edition @ Monday, December 17, 2007 9:53 PM

Merry Christmas to all my reader(s). I hope you have a great Christmas and New Year, and I’ll see you...

Anonymous