I've really been impressed with the
ObjectDataSource control. Not only does it give you a very intuitive way to
provide data binding with your custom object model, but
I found that it had a really useful event model as well. For example, let's say
I have a custom ASP.NET error control that gets passed an exception and the control then displays a
friendly error message to the user depending on the exception type. But what
happens if the ObjectDataSource is bound to a select method that ends up
throwing an exception and I want to handle it on my UI layer? Fortunately, the ObjecctDataSource control has an event named
Selected and another event named
Selecting.
Using the Selected event, I can test the
ObjectDataSourceStatusEventArgs to see if an exception has been thrown, handle
it, and then exit gracefully by setting an
ExceptionHandled flag to true...
//ObjectDataSource
Select method bound here...
public
List<Person>
GetPeople() {
List<Person>
people = new List<Person>();
//force exception here for example...
throw new
InvalidOperationException();
return people;
}
//Page
code-behind here...
protected
void ObjectDataSourcePeople_Selected(object
sender, ObjectDataSourceStatusEventArgs e) {
if (e.Exception != null)
{
//Handle the exception here...
e.ExceptionHandled = true;
}
}
Very cool stuff!