Raise the SNR

August 11, 2009

Accessing Underlying Data in ObjectDataSource – ASP.NET 2.0

Filed under: Development — admin @ 10:59 pm

I went around a few times on this problem today. I was pulling data into a GridView control from an ObjectDataSource. This ODS represented a Generic List of Data Transfer Objects that were populated using NHibernate. The DTO has a property that is really a foreign key into a reference table. The problem is that there are legacy portions of the system that still use the value of the foreign key as data. I know, I know, I should have refactored the relationship into the DTO. Time pressure being what it is, refactoring that portion of the system was not an option. Instead, I wanted to treat the “Order” field as a lookup into the list contained in a second ObjectDataSource. Then I would display the text for the lookup value in the GridView.

Since I dont yet know the internals of databound controls and datasources, I couldn’t find a way to access the data in the ODS programmatically. So, I came up with a huge hack instead. I decided that I needed functionality that looks like a DropDownList control. I had the value in the DTO, and I wanted the human readable text like the Text in a DropDownList. So I created an invisible DropDownList on my page that was bound to a second ObjectDataSource containing the reference data I needed. Then, I set the SelectedValue of the DropDownList to the Order property in the DTO. Finally, I returned the SelectedText of the DropDownList from a protected method that was called using Eval in the GridView’s databound column.

This works, and is much more performant than my first attempt, but there’s got to be a cleaner way to get the desired results.

2 Comments »

  1. Какая фраза… супер, блестящая идея…

    Помощник по хозяйству, техник I went around a few times on this problem today. I was pulling data into a GridView control from an ObjectDataSource…..

    Trackback by Kylie BattName — April 12, 2010 @ 2:18 am

  2. посморим, заценим….

    Обдарована дитина. Ілюзії і реальність I went around a few times on this problem today. I was pulling data into a GridView control from an ObjectDataSource…..

    Trackback by Kylie Batt — April 15, 2010 @ 7:44 pm

RSS feed for comments on this post. TrackBack URL

Leave a comment

Powered by WordPress