Keep IT Simple
Register   |  Login  |  About Us  |  Contact Us  |  Career
More than 9 years experiencs in delivering software solutions for eCommerce, Real Estate ERP. We works with .NET Framework, PHP, and many Open Source solutions. Our solutions will certainly assist you and your business.
Web Design & Web Portal Developments. Our professional designers would certainly help your business create the right branding.
Business Solutions, ERP, CRM, POS. We are able to customize all these system based on your actual need. Never let software changes your workflow. Generate meaningful business reports from one fully customized system.
IT Trainings, including Excel VBA, .NET Programming, Sharepoint developments, Mobile Apps developments. Checkout our offers in training programms.
Knowledge Base
Jun 23

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

 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:
Categories:

5 comment(s) so far...


Gravatar

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
Gravatar

Re: High Performance .NET Programming Practice (1)

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.swtor credits
swtor credits
swtor credits
aion kinah

By runescape gold on   11/3/2011 9:56 AM
Gravatar

Re: High Performance .NET Programming Practice (1)

French archaeologists Denis Vial and Agueda Vilhena Viala, in 1984, the archaeological sites located in shelters on rocks that was called Cave of the Lost, where walls display paintings dated by carbon-14 between 3630 and 4610 years, featuring as Zone Prehistoric.
buy wow gold : www.mmogoldsstore.com/

By ffxiv gil on   11/4/2011 6:08 PM
Gravatar

Re: High Performance .NET Programming Practice (1)

As opposed to making use of its conventional louis vuitton speedy supplies, this season Louis Vuitton has released a brand new series-the Louis Vuitton Slightly Denim. Just as its name tells, the bag is slight in weight for the light denim it made use of cheap louis vuitton and will cheer you up inside the downturn economy with its easy but sophisticated color.Pretty much for every single woman, besides the outfits, handbags are really a further kind of important accessories in her wardrobes. Nowadays, handbags aren't restricted towards Tiffany and CO the standard function of holding every day essentials. Shopping for a Louis Vuitton monogrammed Vernis purse is an investment within your fashion louis vuitton artsy future. The LV and 4 pointed flower monogram are renowned world-wide Tiffany jewelry outlet. In truth, the Louis Vuitton brand name is one of the most pricey names within the globe, products notwithstanding.One thing that you simply need to normally keep in mind regarding the Louis Vuitton brand is that the firm behind coach factory outlet it's a staunch defender of classical aesthetics and conventional high fashion motifs.The bag marries modest capacity and functionality louis vuitton luggage. Firstly, the bag is created from endurable Monogram Denim canvas and not-so-easy-to-stain micro fiber louis vuitton neverfull lining.Actually, they're widely deemed as perfect symbols expressing people's fashion taste and personality.

By uggs outlet on   1/10/2012 5:08 PM
Gravatar

Re: High Performance .NET Programming Practice (1)

using the financial system escalating quickly of China, the lexia 3 market also stays a point go up rapidly, undoubted the car for the the majority of chinese language program experienced been not just a much cry, but in meantime they Renault can clip do not retain up using the development's movement using the element which extremely Digimaster three worth inside the existence safety.

By dsa on   1/30/2012 4:41 PM

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