CodeBetter.Com
CodeBetter.Com
RSS 2.0 via Feedburner
           Do you Twitter? Follow us @CodeBetter

Eric Wise

Business & .NET

ObjectDataSource / Gridview Display Class SubObject Property

How's that for a title?  I am trying to get a the maxinum number of hits for the keywords I'd use on Google for this one since it wasn't all that straightforward.

Let's say you are using the object datasource to return a generic list of address objects.  Inside the address object is a "subobject" called address type which has properties of description and id.

The question is, since the gridview's list of available columns doesn't drill into the subobject, how do I display the description property on AddressType?

The answer lies within the template column.  <minirant>Template columns are a favorite of mine from the old datagrid days in that they allow you to do pretty much whatever your heart desires.  The problem is the usage of them is rather poorly documented so you usually have to go to blogs and newsgroups to find information like this.</minirant>  Anyways, here's the solution:

<asp:TemplateField HeaderText="Type">

       <ItemTemplate>

               <asp:Label ID="lblAddressType" runat="server" Text='<%# ((Address)(Container.DataItem)).AddressType.Description %>'></asp:Label>

       </ItemTemplate>

</asp:TemplateField>

As you can see I'm casting the dataitem as the parent object (Address) and then just calling the property like you would in a codebehind!



Comments

DaRage said:

is this 2 way databinding like <%# Bind() %>?
# March 13, 2006 1:16 PM

Eric Wise said:

Great point Zubair. I'm just always leary of using ToString() except in cases where it's intuitive. Of course for a class that exists to be a describer that would work better.

As for two way databinding I can't say I've tried that. If anyone wants to try it and post do feel free. I personally don't use in grid editing in most circumstances.
# March 13, 2006 2:09 PM

johnwood said:

If you really want to get maximum hits in google, you should put the keywords into the post name (in the advanced settings when you post) so they appear in the url... coz google seems to rank words found in the url higher than page titles. :)
# March 13, 2006 2:50 PM

Pete Nelson said:

Cool, nice to see how this is done! I've always cheated in the past and you'll see stuff like "AddressTypeDescription" on my Address object which drills down into AddressType.Description.

But know I know better. Thanks!
# March 13, 2006 4:30 PM

dotnet_developer said:

How will Two way databing will be achived in this case. This is good to read data from subobject.

But how will EditItemTemplate will look like for two way databinding?

Any ideas!
# May 12, 2006 2:51 PM

Eric Wise said:

Now that I have not tried.  Usually the technique you see me using above is just for display of data.  I generally do not do in grid edits instead pushing the data to a form for edits.
# May 17, 2006 7:07 PM

Leave a Comment

(required)  
(optional)
(required)  

Enter the numbers above:
Add
Check out Devlicio.us!