Binding ObjectDataSource controls to Custom Methods

The new ASP.NET 2 data controls, like the SqlDataSource and the AccessDataSource grealty simplify data access in your Web Applications. However, they have one major drawback: they flood your pages with Sql statements. Not so with the ObjectDataSource, that enables you to bind to the results of a standard method in your Business Logic Layer. The ObjectDataSource can in turn be bound to a data aware control, like the new GridView or DetailsView controls

There are a few ways to bind the ObjectDataSource control to your method. The methods you can use depend on the design of your BLL class and method.

Method 1 - The Default Constructor

If your BLL class has a default, parameterless constructor, you can simply assign the method like this:

<asp:ObjectDataSource ID="ObjectDataSource1" runat="server" 
      SelectMethod="MySelectMethod" 
      TypeName="MyType">
</asp:ObjectDataSource>					

As you can see, you have to define the TypeName (the name of the class in your Business Layer that contains the method you want to call) and the actual name of the method for the SelectMethod property. Before that method is called, the .NET run-time will automatically create an instance of MyType by calling its default constructor (that has no parameters).

The method signature can look as simple as this:

public DataSet MySelectMethod()
{

}  

Method 2 - Static Methods

The next method uses the exact same markup as the first method, but this time the method in the Business Layer is static:

public static DataSet MySelectMethod()
{

}  


With the static method, there is no longer the need to have a default, parameter less constructor on the MyType class. If the class only contains static methods, you can even hide the constructor alltogether by marking it as private.

Method 3 - Using ObjectCreating

The next, and final method I am discussing here, is using the ObjectCreating event. This event is fired by the ObjectDataSource control just before it tries to call your method. Inside this event, you can create an instance of your custom Business class, passing it any type of information it may need to receive in its constructor. Then you assign your custom object to the ObjectInstance property of the ObjectDataSourceEventArgs parameter. The following example shows a simple example of this:

protected void ObjectDataSource1_ObjectCreating(object sender, ObjectDataSourceEventArgs e)
{
  MyType myType = new MyType(1, "SomeValue", SomeCustomObject);
  e.ObjectInstance = myType;
}  

Once this event is done, your custom object myType is then used as the source for the MySelectMethod that is used to bind the ObjectDataSource control. So, under the hood myType.MySelectMethod() is called on your business object to get the data in the ObjectDataSource control.


Where to Next?

Wonder where to go next? You can post a comment on this article.

Doc ID 368
Full URL https://imar.spaanjaars.com/368/binding-objectdatasource-controls-to-custom-methods
Short cut https://imar.spaanjaars.com/368/
Written by Imar Spaanjaars
Date Posted 04/17/2005 20:39
Date Last Reviewed 05/23/2006 20:19
Listened to when writing Escape by Muse (Track 10 from the album: Showbiz)

Comments

Talk Back! Comment on Imar.Spaanjaars.Com

I am interested in what you have to say about this article. Feel free to post any comments, remarks or questions you may have about this article. The Talk Back feature is not meant for technical questions that are not directly related to this article. So, a post like "Hey, can you tell me how I can upload files to a MySQL database in PHP?" is likely to be removed. Also spam and unrealistic job offers will be deleted immediately.

When you post a comment, you have to provide your name and the comment. Your e-mail address is optional and you only need to provide it if you want me to contact you. It will not be displayed along with your comment. I got sick and tired of the comment spam I was receiving, so I have protected this page with a simple calculation exercise. This means that if you want to leave a comment, you'll need to complete the calculation before you hit the Post Comment button.

If you want to object to a comment made by another visitor, be sure to contact me and I'll look into it ASAP. Don't forget to mention the page link, or the Doc ID of the document.

(Plain text only; no HTML or code that looks like HTML or XML. In other words, don't use < and >. Also no links allowed.