Step 2: Add a class file with name = "EmployeeDataAccessLayer.cs". To implement custom paging the following 2 methods are required in EmployeeDataAccessLayer.GetEmployees(int startRowIndex, int maximumRows) - This method is responsible for retrieving the set of required rows. The 2 parameters startRowIndex and maximumRows are used to calculate the correct set of required rows.GetTotalCount() - This method is responsible for returning the total numbers of rows in the underlying table. This method is used by the gridview control to calculate the exact number of pages to display, in the pager area of the gridview control.using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Data;using System.Data.SqlClient;using System.Configuration;

Step 7: At this point, flip webform1.aspx to HTML source mode and delete "SelectParameters" section from "ObjectDataSource1" control. Set the following properties of ObjectDataSource control.MaximumRowsParameterName="maximumRows"StartRowIndexParameterName="startRowIndex"SelectCountMethod="GetTotalCount"EnablePaging="true"

What is the difference between default paging and custom paging?This is a very common asp.net interview question. With default pagingall the rows are retrieved from the database every time we navigate to a different page. For example, if there are 1000 rows in database table, and if the page size is set 10, the gridview control displays just 10 rows at a time, but all the 1000 rows will be retrieved. When we navigate to page 2, the second set of 10 records get displayed in the gridview control, but again all the 1000 rows will be retrieved. Obviously this leads to a lot of unnecessary network traffic between database and web servers and is bad for performance.

With custom paging we only retrieve the subset of rows that will be displayed in the gridview control. For example, if there are 1000 rows in database table, and if the page size is set 10, with custom paging we retrieve just those 10 records. So, custom paging is much better for performance over default paging. When compared with default paging, custom paging is relatively complex to implement.

1 comment:

Hi Venkat,if I understand correctly, the custom paging can be useful to speed up the processing time when the web server and database server are on two different machines and the unnecessary network traffic is time consuming, isn't it? And what about using the cache? Can it help us?I was inspired by your lesson #131 of ASP.NET series (Cache dependency on sql server database table) and I set up another gridview management using the cache. Advantages in this approach, IMHO, are more than one: (1) we need not to implement the custom paging: the default sorting and paging management of ObjectDataSource control is used instead, (2) till the database table is unchanged the cache is used by ObjectDataSource, hence, (3) data retrieving is quick also in case the database & web server are on the same machine, because we need not to prepare the new dataset every time, picking data from database, we just use cached dataset, (4) when the database server is unavailable we can still navigate thru the gridview control and sort it as we like.So, below, I report changes to do: Aspx page design mode: select Gridview1 control, click on upper right arrow -> Edit columns. Select many Boundfields as you like and for each of them set their properties Headertext, SortExpression and DataField. Example: "FirstName" column should be set like this: HeaderText = First Name, SortExpression = FirstName, DataField = FirstName. (By the way, the SortExpression property can contain more then one field separated by comma.)Then select Enable paging & Enable sorting.Add the new label control "lblStatus" on web form.The Select method of the ObjectDataSource1 control should be changed to the EmployeeDataAccessLayer.LoadCachedEmployees()and the data access layer class should be modified like this:

// adding a few more namespacesusing System.Web;using System.Web.Caching;

Eventually, in codebehind code, we have to add an event method for lblStatus control. (Unfortunately lblStatus_PreRender event is fired BEFORE the execution of LoadCachedEmployees(), hence the lblStatus displayed message will always concern the previous stage of loading the grid...)