02 May 2010

Pagination in asp.net

Requirement :
There was a functional; requirement to pull the customer data from the database and display on a web page. But in one query itself will return 1000 records at a time. Seeing 1000 records a t a time was not handy for the user as well as it will be a major hit for the performance aspect. So the requirement was to provide something a kind of page like feeling to the user for the bulk record user where user can see a page at a time where he can go next and previous page same as in a book.

Solutions:
Well, this is not something new to scratch the head. Microsoft has addressed as well as provided solutions for this- nothing but pagination. Basically, you show just one page on screen and provide a navigation bar on top or on bottom of the page (or both) so user can navigate to other pages if wants. The paging can be implemented 2 of the below ways.

• Default Paging – can be implemented by simply checking the Enable Paging option in the data Web control’s smart tag; however, whenever viewing a page of data, the ObjectDataSource retrieves all of the records, even though only a subset of them are displayed in the page
• Custom Paging – improves the performance of default paging by retrieving only those records from the database that need to be displayed for the particular page of data requested by the user; however, custom paging involves a bit more effort to implement than default paging
Default Paging:
To enable default paging we need to explicitly set AllowPaging property of DataGrid control to True. Use PageSize property to set the number of records to be displayed per page. PageSize has a default value of 10. DataGrid control has a property CurrentPageIndex which gives the index of current page being displayed i.e. for the first Page DataGrod1.CurrentPageIndex will be zero. Another property PageCount exposes the total number of pages. In other words PageCount indicates the number of pages in DataGrid control to display all the data from the data source. We will make use of above properties to implement paging in our ASPX page. When the user clicks on a page hyperlink on the ASPX web form, postback operation is performed. On postback, PageIndexChanged Event of DataGrid is called. What all we need to do is implement an eventhandler for the PageIndexChanged event as follows.
Custom Paging:
One problem with Default Paging is, every time we request for a page in the DataGrid, it retrieves complete data from the database. This problem can be a bottle neck if the volume of date we are working with is huge. A genuine suggestion to this problem which comes into mind immediately is to fetch only that data which we need to display on the page. This is what custom paging is all about i.e. fetching only required data from database. To enable Custom paging we need to explicitly set AllowCustomPaging property of DataGrid control to True. Use PageSize property to set the number of records to be displayed per page. We need to set VirtualItemCount property of DataGrid control. This property tells the DataGrid
Pagination using object data source:
I have explored some features of ObjectDataSource control with GridView, which is shipped with ASP.net 2.0. The ObjectDataSource control is used to bind the controls to middle-tier busines objects.it will help us in implementing the custom paging mechanism very easily without the need to build page numbers separately i.e. we can still use inbuilt paging feature of GridView to display page numbers and we can use ObjectDataSource control to fetch the records that is required only for that page.WE have implimented paging using this in our application.Here for example given below we need to pass the parameteres of startindex,pagesize as well as page size as a parameter to selected method as well as sql query.Here also I have passed sort directins also.






No comments: