ObjectDataSource Control

Posted Tuesday, June 14, 2005 9:17 AM by C-Dog's .NET Tip of the Day
In continuing our discussion on databinding, we will move to the ObjectDataSource.  This is by far one of the best controls they have added.  It allows you to bind data to controls from custom user objects.  Typically in our case this would be used to bind our data to a web service call.  It works much in the same way as the SqlDataSource boject.  When you drag one onto the page, a SmartTag allows you to specify which object to bind to and then it prompts you to select which method to use for Select, Update, Insert, and Delete operations.
 
The ObjectDataSource supports many of the same parameters as the SqlDataSource.   The SelectMethod property specifies which method to call to perform a select operation.  The SelectParameters property contains all of the parameters to the SelectMethod (again these can be a control, session, profile, cookie, etc).  If you want to support paging, then you will need to specify a method with SelectCountMethod.  This method will be used to get the number of rows returned by a select.  There are also matching properties for Insert, Update, and Delete.
Here is a simple example, that is used by CarDashboard.  You can view all the code in Source Control at $/Thrifty.NETv2.0b2/WebAdministration.
<asp:ObjectDataSource ID="VehicleImageObjectDataSource" Runat="server" 
TypeName="Thrifty.Components.Web.Locations.Resources.Locations"
SelectMethod="GetVehicleImages" SelectCountMethod="GetVehicleImagesCount" 
EnableCaching="false"CacheDuration="180" 
OnSelecting="VehicleImageObjectDataSource_Selecting">
</asp:ObjectDataSource>

Read the complete post at http://www.dotnettipoftheday.com/blog.aspx?id=111