Blog Home  Home Feed your aggregator (RSS 2.0)  
Some questions and answers about the ObjectDataSource - Manuel Abadia's ASP.NET stuff
 
# Sunday, 16 July 2006

Here are some common questions and answers about the ObjectDataSource.

Why my SelectCountMethod needs a StartRowIndex and MaximumRows parameters in order to work properly?

The answer is that they are not needed. However, the ObjectDataSource will call the SelectCountMethod with all the parameters present in the SelectParameters collection. If you have enabled paging in the ObjectDataSource, the StartRowIndex and MaximumRows parameters will be added to the signature of the SelectMethod even if they are not in the SelectParameters collection. Usually the StartRowIndex and MaximumRows parameters will be set automatically by the control bound to the ObjectDataSource so there is no need to have them in the SelectParameters collection so if you remove them the SelectCountMethod will not need them. The same happens to the SortParameter. Most examples take the StartRowIndex and MaximumRows because when the method is selected in design time, all the parameters are persisted as parameters in the SelectParameters collection.

Can I use an interface as the TypeName in the ObjectDataSource?

The ObjectDataSource designer will not let you choose an interface in the first step of the wizard, and if you have specified the TypeName in the ASPX and you then run the designer, the methods associated to the interface will not be shown, so if you want to use interfaces with the ObjectDataSource you lost design time support. If you configure the ObjectDataSource to use the methods from an interface a runtime error will happen:

Cannot create an instance of an interface.

because the ObjectDataSource will try to create an instance of the Type specified in the TypeName property (if the method to call is an instance method). However if you handle the ObjectCreating event, you can create an instance of an object that implements the interface specified by the TypeName property and assign it to the ObjectInstance property of the event arguments. That will make everything will work as expected.

I have a GridView bound to an ObjectDataSource. I have a SelectParameter for the sorting with a DefaultValue specified. However, when the page initially loads the DefaultValue is never used. Why?

When the GridView needs data, a select operation is performed in the underlying data source control. Before this operation is performed, a method called CreateDataSourceSelectArguments is called on the GridView. This method creates the arguments that will be passed to the Select method of the ObjectDataSource, which in turns calls the method specified by the property SelectMethod. In the CreateDataSourceSelectArguments method, the SortExpression is set based on the GridView’s state and the ObjectDataSource just assign that value to the sort parameter. So, as you can see, the DefaultValue is never used for the sort parameter.

Sunday, 16 July 2006 17:30:25 (Romance Daylight Time, UTC+02:00)  #    Comments [0]   ASP.NET  | 
Copyright © 2018 Manuel Abadia. All rights reserved.
DasBlog 'Portal' theme by Johnny Hughes.