Jason
Jason

Reputation: 52523

Bind to a property's property

I have a List (Of MyObject) binding to a GridView in the Page_Load event. MyObject has a child object as a property, ChildObject which also has a property itself, ChildProperty.

In my GridView, I would like to bind one of the fields to ChildProperty, but doing something like:

<asp:boundfield datafield="ChildObject.ChildProperty" />

results in an error:

System.Web.HttpException: A field or property with the name 'ChildObject.ChildProperty' was not found on the selected data source.

How do I bind to that property, or is it not possible? I suppose I could create a ReadOnly property in my parent object just to read the child property, but that's a bit smelly.

Any ideas?

Thanks!

Upvotes: 2

Views: 1299

Answers (2)

Fer
Fer

Reputation: 1

In an ItemTemplate I would use:

<%# (Eval("ChildObject") as objectType).ChildProperty #>

Upvotes: -1

Rex M
Rex M

Reputation: 144112

You can use:

<asp:TemplateField>
    <ItemTemplate>
    <%# DataBinder.Eval(Container, "DataItem.Property.ChildProperty.GrandChildProperty") %>
    </ItemTemplate>
</asp:TemplateField>

Upvotes: 5

Related Questions