user1434739
user1434739

Reputation: 235

Accessing ASP.NET C# Eval Databound Object within Object

I have a rather tricky situation. I am using an ASP:Repeater which is bound to a SOAP datasource returning the below object structure.

Path: Array[6]
0: Object
   Direction: "Departing"
   Message: ""
   Operator: "myciti"
   RouteColor: "#a7a9ac"
   RouteName: "Airport"
       Stop: Object
           Name: "JFK"
           Location: Object
   Name: "Airport"
   __proto__: Object
   Time: "07:10"
   Type: ""
   Vehicle: ""

I can easily access all the first tier variables, such as "Time" by using Eval as such:

  <%# Eval("Time")%>

My problem is how would I access the nested objects still using EVAL from the ASPX page? For example how would I get to the "Name" value of the "Stop" object within this array which has the value "JFK"?

Upvotes: 2

Views: 4089

Answers (2)

Carlos Martinez T
Carlos Martinez T

Reputation: 6528

Eval is just a shortcut to obtain a property by name. It won't help you in some cases. Like for example, what if your object RouteName sometimes returns null.

You need to use:

<%# Container.DataItem.RouteName.Stop.Name %>

Container.DataItem is an object so you can access just about any property from it.

A better way would be to Cast that object into a more meaningful instance:

<%# Ctype(Container.DataItem, ReturnedSOAPObjectRecord).RouteName.Stop.Name %>

This method gives you the ability to check for nulls.

<%# IF(Ctype(Container.DataItem, ReturnedSOAPObjectRecord).RouteName IsNot Nothing, Ctype(Container.DataItem, ReturnedSOAPObjectRecord).RouteName.Stop.Name, "No Stop" %>

Upvotes: 2

AntLaC
AntLaC

Reputation: 1225

Try: <%# Eval("Stop.Name")%>

Upvotes: 6

Related Questions