Here I am , with my first post. 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.
There are two ways , through which we can implement the paging functionality into our application.
- Use the paging behaviour of the control (e.g. GridView)
- 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.
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.
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.
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.
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.
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.
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.
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.
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.
- Add GridView
- Add ODS
- Add Class which represents the business object.
- Set ODs.TypeName to Class name which represents the Business object.
- Set ODS.SelectMethod name to name of the method which return data
- Set ODS.SelectCountMethod to name of the method which return row count
- Set ODS.StartRowIndexParameterName and ODS.MaximumRowsParameterName
- Modify the signature of your select method to include above two parameters.
- Implement the SelectMethod and SelectCountMethod
- Set the GridView.DataSourceID to Id of ODS.
- Run the page in browser
Unfortunately, wordpress doesnot allow the upload to zip files. So , if you need the code, leave a comment.
I will be coming up with more on ODS and GridView next time. So, stay sharp ….