Using a DataPager with the GridView Control - Implementing IPageableItemContainer

One of the new controls that shipped with ASP.NET 3.5 is the DataPager.  It's a flexible control that renders the paging elements data-bound controls that implement the IPageableItemContainer interface (currently only the ListView control).  The big change with the DataPager is that it is decoupled from the data-bound control it provides paging support for.  It can be embedded somewhere within the data-bound control's control hierarchy (like contained within the LayoutTemplate of the ListView), but it doesn't have to - It can exist on its own outside of the data-bound control as well.

When I was writing my last post, I kind of wished the 3.5 GridView implemented the IPageableItemContainer interface.  It sure would have made things simpler for me.  So I thought I would take a shot at extending the GridView myself.  Read on for the details.

Live Demo (IE6, IE7, FF, Opera) | Download     

image

Implementing IPageableItemContainer

This is a pretty simple interface - 2 properties, a method and an event.  But I didn't even try to implement it on my own!  Instead I opened Reflector and took a peek at the ListView's implementation. 

Here is the specification of the IPageableItemContainer interface.  

image 

I implemented the MaximumRows getter to return the GridView's PageSize value and had the StartRowIndex return the PageSize multiplied by the PageIndex.  For the TotalRowCountAvailable event, I followed the same pattern the other controls use for declaring events.  It is a little weird if you haven't seen this syntax before, but it uses less memory and is the pattern the other web controls use so I followed it.

image

The SetPageProperties method is only mildly more difficult (again with the help of the ListView's implementation as a reference).  When the SetPageProperties method is invoked, it is passed the new startRowIndex, the new pageSize (what the DataPager refers to as maxmimumRows) and a boolean value that is true if the control needs to be rebound after the properties are set.  Armed with these new values, my SetPageProperties implementation updates the GridView's PageSize and PageIndex properties and raises the PageIndexChanging and PageIndexChanged events along the way.

image

Override GridView.CreateChildControls

After the interface is implemented, the only thing that remains is figuring out when my new GridView needs to raise the TotalRowCountAvailable event.  It turns out the CreateChildControls method is where the ListView control raises this event (again, figured out that using Reflector and taking a look at how the ListView control does it).  So I overrode the CreateChildControls method in my new GridView class and added logic that raises the TotalRowCountAvailable event.  After the event is raised, I go ahead and make sure the GridView isn't rendering the pager rows.  Note: my extended GridView doesn't support the standard GridView paging rows anymore, only the DataPager is supported.  This is why I have made sure the pager rows are not visible.

 image

Is This Really All That Useful?

I think it might be.  Sometime in the next year, one of the projects I am working on will be converting from ASP.NET 2.0 to 3.5.  If I had to guess I would say we have somewhere around 75 or so GridViews and a ton of GridView specific code for viewing, inserting, updating and deleting records that has already been written and tested.  Having the GridView implement IPageableItemContainer gives me flexibility and provides me choices when we start the migration project - I can keep all of my GridView code, but completely change the way we present our paging elements. 

I am not sure if we will actually go this route, but I figured I would post it - just in case someone else can improve or make use of it.

That's it.  Enjoy!  

P.S  I have another vacation coming up.  This time I am on to Augusta to watch the Masters and then some more traveling the following week.  So things will probably go quiet around here for the next few weeks. 


TrackBack

TrackBack URL for this entry:
http://mattberseth.com/blog-mt/mt-tb.fcgi/115

Listed below are links to weblogs that reference Using a DataPager with the GridView Control - Implementing IPageableItemContainer:

» April 11th Links: ASP.NET, ASP.NET AJAX, ASP.NET MVC, Visual Studio, Silverlight from Mirrored Blogs
Here is the latest in my link-listing series .  Also check out my ASP.NET Tips, Tricks and Tutorials [Read More]

» Апрель, 11е. Ссылки: ASP.NET, ASP.NET AJAX, ASP.NET MVC, Visual Studio, Silverlight from Scott Guthrie's Blog in Russian
Вот здесь можно посмотреть предыдущий пост из серии списка постов. Также некоторые ссылки на мои популярные [Read More]

» 4月11日链接篇: ASP.NET, ASP.NET AJAX, ASP.NET MVC, Visual Studio, Silverlight from Joycode@Ab110.com
【原文地址】 April 11th Links: ASP.NET, ASP.NET AJAX, ASP.NET MVC, Visual Studio, Silverlight 【原文发表日期】 Friday [Read More]

» لینک های یازدهم آوریل: ASP.NET,ASP.NET AJAX,ASP.NET MVC, Visual Studio,Silver Light from وبلاگ اسکات گوسری به زبان فارسی
ASP.NET: آموزش های امنیت بیشتر در ASP.NET : سه آموزش آخر و عالی اسکات میشل در مورد امنیت ASP.NET سه آموزش [Read More]

Comments


The only thing I notice in the example is that after an sort is clicked the page goes back to page 1 instead of the page where I was.

Have fun with your holliday!
Enjoy...

It is useful Matt, because if someone is not going to use it with GridView, I guess there are many other scenarios where IPageableItemContainer can be implemented and used. You just provided us with some good example!
Thank you

Posted by: Derek Klingman on April 9, 2008 12:00 AM

Great post! Just before the 3.5 framework came out I had created my own paging control that I could just drop into the gridviews paging template. Its great to see that I will be able to use the datapager with less implementation. Thanks Matt!!!

Posted by: Kasim on April 10, 2008 12:00 AM

Good staff as usual.
Thanx.

PLease I want the gridview to be in the middle(center) any suggestion. Thanks

Great DataPager reference.

Posted by: Natalie Zhuravleva on August 14, 2008 09:44 AM

I have used your example for my GridView but the sorting functionality doesn't work. It throws an exception:

Sys.WebForms.PageRequestManagerServerErrorException: Exception has been thrown by the target of an invocation.

I googled it and some other posts mentioned the system trying to look into App_Data but I am using an ObjectDataSource to pull my data.

Could you suggest a workaround?

Thanks

thx, good solution for rich apps with data paging.

Posted by: georgi on September 19, 2008 06:02 AM

hello i have a question about bingding the grid.If i bind it like here in the example there is no problem, i mean with SqlDataSource control.But if i bind it with something else like IQueryable (linq) there is some problem with the onPageChanging event it isn't handled

we have been looking something similar for our site which is in ASP.Net 2.0. Can you suggest something.

Post a comment

(If you haven't left a comment here before, you may need to be approved by the site owner before your comment will appear. Until then, it won't appear on the entry. Thanks for waiting.)

Consulting Services

Yep - I also offer consulting services. And heck, I'll do just about anything. If you enjoy my blog just drop me an email describing the work you need done.

Recent Comments

  • wish wrote: we have been looking something similar for our site which is in ASP.Net 2.0. Can you suggest somethi...
  • georgi wrote: hello i have a question about bingding the grid.If i bind it like here in the example there is no pr...
  • Guf wrote: thx, good solution for rich apps with data paging....
  • Natalie Zhuravleva wrote: I have used your example for my GridView but the sorting functionality doesn't work. It throws an ex...
  • asp.net wrote: Great DataPager reference....
  • Ruth wrote: PLease I want the gridview to be in the middle(center) any suggestion. Thanks ...
  • Kasim wrote: Good staff as usual. Thanx. ...
  • Bjorn van der Neut wrote: The only thing I notice in the example is that after an sort is clicked the page goes back to page 1...