Building Custom Paging with LINQ, ListView, DataPager and ObjectDataSource, Different Paging Method

by mosessaur| 08 July 2008| 10 Comments

Yesterday, I was reading Efficient Paging in SQL Server 2005 post by Justin Etheredge, then in the comments I found Kevin Hazzard pointing to his Efficient LINQ to SQL custom paging approach. I liked his way, which reflects Justin's method which I already seen before but never apply it. I think it is more efficient than my method I provided in an earlier post. And if you googled about custom paging with LINQ to SQL you'll find few more implementation.

Today, I'm updating my method and provide the same sample I made last month with Kevin's method. I made an extension method to IQueryable Interface and called it KavinPage. You can view a demo here

Here is the code for the Extension Method:

   1: public static IQueryable<T> KevinPage<T, TResult>(this IQueryable<T> obj, int page, int pageSize, System.Linq.Expressions.Expression<Func<T, TResult>> keySelector, bool asc, out int rowsCount)
   2: {
   3:     rowsCount = obj.Count();
   4:     if (asc)
   5:     {
   6:         return obj.OrderBy(keySelector).Skip(page * pageSize).Take(pageSize);
   7:     }
   8:     else
   9:     {
  10:         return obj.OrderByDescending(keySelector).Skip(page * pageSize).Take(pageSize);
  11:     }        
  12: }

It is all about Extension Methods and how to use them to build SQL Queries. You can return for Kevin's post for detailed explanation. Also you can return to my previous post about this subject for more details about how to put it all together to build Paging ASP.NET ListView using LINQ to SQL and ObjectDataSource.

Download the updated sample.

kick it on DotNetKicks.com

Comments (10) -

Kevin Hazzard, MVP
Kevin Hazzard, MVP United States on 7/2/2008 11:26 AM Nicely done Moses. Clean and simple. And efficient to boot! I've never had a class named after me.

Thanks,

Kevin
mosessaur
mosessaur Egypt on 7/2/2008 11:25 PM @Kevin Thank you Kevin, I was happy to know much efficient way. And I wished to show that no matter how do we implement paging, we can pack our code in something reusable.
Cheers
Bharath Reddy VasiReddy
Bharath Reddy VasiReddy United States on 7/3/2008 10:09 AM Hi Mosa Soliman,

     I am facing one problem with paging with ListView control usig LINQ. When I am tryig to perform paging on the ListView control, its not showing the second page records. If I am cliking for the second time its showing the second page data, but when i am trying to perform another paging then i am getting a script error.
  
Microsoft Jscript runtime error:
Sys.WedForms.PageRequestManagerServerErrorException: Failed to load viewstate. The control tree into which viewstate is being loaded must match the control tree that was used to save viewstate during the previous request. For example, when adding controls dynamically, the controls added during a post-back must match the type and position of the controls added during the initial request.

I am not using any dyanmic control in my page.
Can you please help me finding a solution for the above problem.
mosessaur
mosessaur Egypt on 7/3/2008 10:40 AM @Bharath, I need to look at your code! can you please send it to me!
You can use contact link to send me and e-mail with code attached.
But I'm sure that the code here in this sample works fine with you! Isn't it?!
Ashok
Ashok India on 11/29/2008 4:08 PM Hi Mosa
i want paging,sorting in gridview with ProgressBar(jquery or update panel with ajax),but i want use stored procedure who take some parameter(Pagesize,pageNo,oderby,output parameter TotalRecord),this proc call with linq(on .cs codebehind) and datapager use paging.
mosessaur
mosessaur Egypt on 12/1/2008 1:20 AM @Ashok Just create your procedure add it to your LINQ to SQL and call it from your context!
Fernando
Fernando United States on 12/13/2008 3:49 AM Great piece of code! It's awesome.  I implemented into my Listview,but when I sort by a certain column, sometimes it brings back the same products for the 4,5,6,7,8... etc. pages.  It doesn't actually page to the next records, instead in displays the same records for 5 or 6 more pages. The only modification I made to your code was used my tablename. Any ideas what might be causing this?

Thanks.
mosessaur
mosessaur Egypt on 12/13/2008 4:52 AM @Fernando I need to look into the code maybe there is something you are missing or maybe doing something you don't notice!
Fernando
Fernando on 12/15/2008 3:23 PM Moses, other question I have is how can I return to the same page number on the listview if I go to another page on the site then return to the page where the listview is at?
Azmathullah
Azmathullah India on 1/20/2009 9:17 PM I am working in .Net Technologies, i am planning to learn share point server Can u suggest me how to start.

Pingbacks and trackbacks (3)+

Comments are closed