Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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"?

like image 809
user1434739 Avatar asked Dec 19 '25 00:12

user1434739


2 Answers

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

like image 115
AntLaC Avatar answered Dec 20 '25 14:12

AntLaC


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" %>
like image 30
Carlos Martinez T Avatar answered Dec 20 '25 12:12

Carlos Martinez T



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!