Friday, 20 July 2007

ASP.NET Details View and Object Source

Using an Object Source can give funny little quirks.

For example if binding to an object you can bind to a property as normal. If you wish to display a property which is a member of an object which is a member of the object you are binding to (it complains).

Here is a simple way around it:

<asp:DetailsView ID="dv" runat="server" AutoGenerateRows="False">
<asp:BoundField DataField="MyProperty" HeaderText="MyProperty" />
<asp:TemplateField HeaderText="My Object Property"><ItemTemplate><%# Eval("MyObject.MyObjectProperty")%></ItemTemplate></asp:TemplateField>

Another quick point if you wish to bind an object to a details view (for example) using object source. Just add the object to a list.

Here is a VB sample showing what i mean (I like to switch examples around occasionally):

Dim myItems As New List(Of Item)

dv.DataSource = tasks

No comments: