ASP.NET Databinding lazy properties

Every since using NHibernate, the effort required to use Many-to-many and one-to-many joins in business objects is a brainless exercise. When using a lazy loaded property on a business object, it just works, however, what happens when you try to bind this property using an ObjectDataSource hooked up to gridview or formview in asp.net...

image

As seen above, the GridView works out that it's out of it's depth with all the complex properties and only defines the simple text/int/boolean columns. The only alternative for the complex properties is to define your own template columns.

To display the value of a complex property you are still able to access the sub-properties using the Eval() command. So in the example below, I can use Eval() to display the name of the 'parent location' that is currently selected. To actually select the location, I can bind it the a custom user control using the Bind() method, this will pass in the entire lazy loaded 'Location' object. The user control must then pass back out a Location object when the ObjectDataSource needs to do an update.

image 

After making this change my grid now has the lazy loaded 'Parent Location' property:

image

When the grid is in action, view mode:

image

Edit mode:

image

Although this is a relatively simple example, it does show how much code you don't need to write to do a one-to-many join using binding in asp.net 2.0.

NHibernate
Posted by: Brendan Kowitz
Last revised: 21 Sep 2013 12:15PM

Comments

No comments yet. Be the first!

No new comments are allowed on this post.