Keep IT Simple
Register   |  Login  |  About Us
Knowledge Base
Jun 23

Written by: Timothy Mo
6/23/2009 4:49 PM 

 Last year I conducted a half day training for a team of developers. The topic was about high performance programming with ASP.NET.

I feel the topic was quite interesting, so before I forget, I should write it down.

This article is about the paging with datagrid.

What is Paging?

Every web developer should at least know what is paging, which is a way to display lots of data by dividing into different pages, and each page should have a fixed size, say, 10 records per page.

Just go to Google.com, do a simple search, that is a live demo for the paging.

Coding paging is very painful process in old ASP 3.0 years. Try coding one if you don't believe. Basically you have to prepare all the page numbers, links for previous page & next page, etc. Btw, code html table to display records also not easy.

So come to .NET time, there is this control called DataGrid (now GridView), which seems to get rid of all those painful programming process by doing some simple settings, like, EnablePaging, PageSize, etc. Too good to be true?

Paging with DataGrid (GridView), the Default Behavior

The steps to use datagrid/gridview to do paging is simple:

  1. Enable property "AllowPaging" and give a value to "PageSize", say, 10.
  2. Choose some property settings for page numbers.
  3. Bind a datasource to the datagrid as usual.
  4.  Done. You should be able to get your records displayed properly in paging view.

Simple to do, but it creates problems.

Issues with The Default Paging Behavior

The main problem with default paging is that the datagrid will become the performance bottle neck in your application.

Imagine you have a 1000 records for a query, and your page size is 10. 

So in this query you should expect 100 pages.

Here is the problem, the default paging will request all 1000 records to be queried out so that the datagrid can actually do the rendering to generate the proper html view.

And users of the application expect to view only 10 records (current page only), but every time they have to query 1000 records out.

And the next problem is, you have to determine whether you want to cache the query result or not. If you store in session or viewstate, you will create an even large problem. If you don't store, that means you have to query database again and again for that 1000 records whenever you click something.

So what should be the solution?

 

Do You Own Paging

There is this property "AllowCustomPaging". Custom paging basically means you have to build all the page numbers by setting some properties.

For example, you need to specify the total number of records. 

The best thing is, you can choose to query only records you want to display.

Assuming for the same 1000 records, you want to display the 5th 10 records only. So you query your database for that 10 records and send to the application layer. From there you bind your datagrid/gridview, with only 10 records. It is going to be much faster. And imagine if the number of records grow to 10,00000? 

Default paging will fail you during performance testing for that.

How To Implement Custom Paging

There are tons of good articles teaching you how to do custom paging. Over here I will just summarize the key points and I will give reference to those good articles.

The first thing, you must know how to do paging with Stored Procedure. You will need to create a Stored Procedure with parameters to support paging. Parameters like, pagesize, pagenumber, must be supported by the Stored Procedure.

There is a good article to teach you about that: http://www.4guysfromrolla.com/webtech/062899-1.shtml 

Then, you need to program your datagrid/gridview to support the custom paging. Basically there are some simple settings to set and a bit additional coding to handle page numbers.

And this article is a good reference to custom paging implementation: http://www.c-sharpcorner.com/UploadFile/mosessaur/custompaging08202006185153PM/custompaging.aspx

Paging with LINQ

Now there is relatively easier way to do paging without fetching all the records. 

Some sample code like this:

//used by LINQ to SQL

public static IQueryable Page(thisIQueryable source, int page, int pageSize)

{

return source.Skip((page - 1)*pageSize).Take(pageSize);

}

//used by LINQ

public static IEnumerable Page(thisIEnumerable source, int page, int pageSize)

{

return source.Skip((page - 1)*pageSize).Take(pageSize);

}

From the code, you can easily see the concept is very similar to what we did in Stored Procedure. 

The complete article, please read here: http://solidcoding.blogspot.com/2007/11/paging-with-linq.html

 

Tags:

1 comment(s) so far...

Re: High Performance .NET Programming Practice (1)

Good to read interesting posts on your blog. Thanks.
www.rapidsharemix.com

By Odysseus on   12/23/2009 9:21 PM

Your name:
Your email:
(Optional) Email used only to show Gravatar.
Your website:
Title:
Comment:
Add Comment   Cancel 
Knowledge Base
Knowledge Base
Knowledge Base
Privacy Statement Terms Of UseCopyright 2009 by wGrow Technologies Pte Ltd