Apr 18 2006

Paging With ObjectDataSource in One Trip

Category: ASP.NET 2.0 - GeneralBil@l @ 11:55

This post assumes you have the previous post on the DataObjectTypeName and ObjectDataSource .

Usually, when we want to allow paging when we are using ObjectDataSource, we should enable paging in both the Data-Bound Control (GridView an example) and the ObjectDataSource.

  • GridView: AllowPaging and off course you need to set PageSize.
  • ObjectDataSource: EnablePaging, also you need to set the:
    • MaximumRowsParameterName="maxRows"
    • StartRowIndexParameterName="startRowIndex"
    • SelectCountMethod="RecordCount"

The method specified in the SelectMethod should now have 2 parameters:

  • int maxRows
  • int startRowIndex

The key point I wanted to show here is the following:

When you want to allow paging, you should add a method called SelectCountMethod, this method is used by the ObjectDataSource to know how many pages there will be.

An old conception for me has been that, we are doing two database round-trips. One to get the data and one to get the Record Count.

This is not true anymore!!! Why?

Let me explain, if the methods in the data mapper class are instance methods that is, not defined with static methods, then ODS will create an instance of the object specified in the TypeName, and access those methods, then disposes that instance.

However, if we had enabled paging, and both the SelectMethod and SelectCountMethod are both instance methods, then, the same instance will be used to access those two methods. So now, inside your Select method, you can get the count of the total records, and store it in a private instance, and then have a SelectCountMethod return that instance. This way, in one shot, you got both values, and since the "same instance" is created by the ODS, means you can for sure make use of the value of the record count, check this sample:

   [DataObjectMethodAttribute(DataObjectMethodType.Select, true)]
    public List<Employee> Get()
        // your logic to retrieve all records

        // Get the Count record
        _CountRecords = (int)dr["Count"];

        return new List<Employee>();

    public int RecordCount()
        return _CountRecords;

Hope this helps you,



Comments are closed