Click here to Skip to main content
Click here to Skip to main content
Go to top

Implement Paging using ObjectDataSource with GridView

, 30 Apr 2006
Rate this:
Please Sign up or sign in to vote.
Demonstration of paging and Sorting with example , using ObjectDataSource and GridView

Introduction

In this article, 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. I have demonstrated some of the features of ObjectDataSource control with the help of an example, for which I could have easily used XMLDataSource control. I did this because, when you download some thing from internet saying connect to database. This database connectivity always causes some problems, with some of you. So, for the purpose of keeping the things easier for you, I have used ObjectDataSource(it will be called 'ODS' for the rest of the article)  control with XMLFile.

ObjectDataSource::Paging

There are two ways through which we can implement paging in our application:

  1. Use the paging behaviour of the control (e.g. GridView)
  2. User the paging at Datasource level.

The first approach leads to the fact that the control loads all the data from data source , then displays selected records based on current page number and page size of the control. This means that a lot of data has to be travelled across the network, which may be decrease the efficiency of the application. To implement this functionality, we just need to set EnablePaging and PageSize properties on the GridView

To limit the amount of data which is going to be travelling on the network, ASP.net 2.0 supports the paging at DataSource level i.e. we will be sending only the data which is required to be displayed at the control.

The beauty of ASP.net 2.0 is that you can write "Zero Code" websites, with very very little modification to business objects. Most of the magic is done behind the scenes , by .net framework 2.0. You dont need to write that much code for it, once you understand this magic. All of the stuff can be done by just setting the appropriate properties of data source controls.

To enable paging at datasource level, you need to set EnablePaging property of ODS. There are some properties which need to be shed some light on.

  1. TypeName
  2. SelectMethodName
  3. SelectCountMethod
  4. StartRowIndexParameterName
  5. MaximumRowsParameterName

ObjectDataSource.TypeName

This property represents the fully qualified name of the business object , which is going to return us the data we will be displaying in the control. Its a class name of the middle-tier object. If you consider , default.aspx : The ProductsList class in the App_Code folder is the one, which we willl be using to get data.

ObjectDataSource.SelectMethodName

This property represents the name of the method of the business object , which is going to return us the data we will be displaying in the control. If you consider , default.aspx : The GetProducts() method of ProductsList class in the App_Code folder is the one, which we willl be using to get data.

ObjectDataSource.SelectCountMethodName

This property represents the name of the method of the business object , which is going to return us Count of the total numbers of rows of the data, we will be displaying in the control. If you consider , default.aspx : The GetRowCount() method of ProductsList class in the App_Code folder is the one, which we willl be using to get Count of the data rows.

ObjectDataSource.StartRowIndexParameterName

This property represents the name of the parameter, which represents the first row of the subset of data, we are going to display on the page.

ObjectDataSource.MaximumRowsParameterName

This property represents the name of the parameter, which represents the maximum rows, we are going to display on the page.

 There is also a property named "SelectParameters", which represents the paramters which we are going to pass to the method ,which is specified in "SelectMethodName" property of ODS. These parameter values can be obtained from any thing ranging from form control value to cookie value.

Parameter Passing

How the parameters to the select method will be passed? If  EnablePaging property of ODS is set , then the parameters specified in the property of ObjectDataSource.StartRowIndexParameterName and ObjectDataSource.MaximumRowsParameterName are passed to the select method and in the same order.Therefore, the signature of your SelectMethod must contain these two paramter names.e.g

ProductsList.GetProducts(int StartRow,int PageSize)

 If you want to pass any other parameter in Select Method, add it in SelectParameters property of ODS. This parameter will be passed before the two parameters , which are mentioned above.

You can see the signatures of ProductsList.GetProducts() method which contains two parameters , whose names are specified in ODS.StartRowIndexParameterName and ODS.MaximumRowsParameterName properties. Now, if you want any additional paramter to be passed, which are needed for your own business logic, add it in ODS.SelectParamters property and modify the signature of ProductsList.GetProducts() as follows:

DataTable GetProducts(MyNewParameter,StartRow,PageSize)

MyNewParameter is that parameter which is added in ODS.SelectParameters property.

Another interesting fact is that you dont need to pass values for StartRow and PageSize  parameters manually. The .net framework automatically pass the values in these paramters such that StartRow parameter will contain starting row index for the current page and PageSize will be passed the value of GridView.PageSize property. (Note: It is not necessary to name the second paramter as PageSize. It can be any other parameter name and yet the value will be passed by .net framework correctly).

Since, we are returning only those rows which are to be displayed on the current page, how will the gridview know the total number of rows in the data source? The answer is using the method which is specified in ODS.SelectCountMethod property. This method takes no parameters and returns integer.
This method should return the total number of rows in the data source so that GridView could set its pager appropriately. For example, if there are 20 rows in the table and PageSize of GridView is set to 10 and method named specified in ODs.SelectCountMethod returns 10 , then GridView wont show the paging indicators, since it will only see 10 rows which are equal to its PageSize.

ObjectDataSource Events

ODS has two events which are important in this context. The ODS has Selecting Event which is fired before it fetches data from Business object and ODS.Selected event is fired when data has been fetched.
The selecting event is fired two times every time when data is retrieved. One for data fetching and for getting row count. You can check this by checking the e.ExecutingSelectCount property. It returns true if row count is being retrieved else false.

So, for every data fetch, there are actually two trips to business object, one for fetching data and  the next is for getting row count . One time the SelectMethod will be executed  and other time the SelectRowCountMethod, respectively.

ObjectDataSource::Sorting

GridView automatically sorts the data for you. There is no need to take any special steps for it. Just Set the AllowSorting property of GridView to true.
ODS has event named Sorting , which is fired before the sorting takes place.

Below are the steps you need to perform to setup your page for paging and sorting using ODS and GridView.

  1. Add GridView
  2. Add ODS
  3. Add Class which will return data
  4. Set ODS.SelectMethod name to name of the method which return data
  5. Set ODS.SelectCountMethod to name of the method which return row count
  6. Set ODS.StartRowIndexParameterName  and ODS.MaximumRowsParameterName
  7. Modify the signature of your select method to include above two parameters.
  8. Implement the SelectMethod and SelectCountMethod
  9. Set the GridView.DataSourceID to Id of ODS.
  10. Run the  page in browser

I will be coming up with more on ObjectDataSource. So Stay sharp... 

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

KSAhmed
Web Developer
United States United States
I m a software developer working for about 2 years in the software industry. I have been focused primarily on .NET
 
I love to work develop for web using new technologies.
 
You will see more articles coming up about ASP.net 2.0.

Comments and Discussions

 
GeneralMy vote of 5 Pinmemberlinhdanhthue11-May-13 17:22 
QuestionRating PinmemberItByAinsworth1-Aug-12 13:06 
GeneralMy vote of 5 PinmemberHINEEL2-Feb-11 23:39 
GeneralReason for this error message ..could not find PinmemberMember 436622021-Aug-09 11:23 
Generalgetting error while calling function to use inpagging. PinmemberNetweb28-Jul-09 23:27 
GeneralThis can be done with much less work IMO. PinmemberDkadoicster29-Nov-08 4:02 
GeneralThanks for the article PinmemberRajiv Gowda14-Aug-08 16:56 
Questionhow to insert parameter in GetRowCount(string para1) PinmemberVT81687-Dec-07 17:30 
Questionparameters? PinmemberHenrique Duarte13-Aug-07 4:51 
AnswerRe: parameters? PinmemberKSAhmed13-Aug-07 5:42 
QuestionGridview malfunctions - Please help me out PinmemberHasan Ali25-May-07 3:33 
Questionwhy sorting dosn't work properly Pinmemberahmadartoflove30-Aug-06 22:52 
AnswerRe: why sorting dosn't work properly [modified] Pinmembermero9099-Feb-07 4:28 
QuestionAny idea how to pass a non-string to a parameter? Pinmemberwyx200015-Jun-06 9:06 
AnswerRe: Any idea how to pass a non-string to a parameter? PinmemberKSAhmed1-Feb-07 17:25 
Generalthanks Pinmemberhsb030712-May-06 13:31 
Generalsource file don't dDownload Pinmemberhsb03072-May-06 12:28 
AnswerRe: source file don't dDownload PinmemberKSAhmed3-May-06 8:15 
GeneralRe: source file don't dDownload PinstaffSmitha Vijayan11-May-06 5:00 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web02 | 2.8.140916.1 | Last Updated 1 May 2006
Article Copyright 2006 by KSAhmed
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid