Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Binding a drop down list to properties in child objects

Status
Not open for further replies.

johnturgoose

Technical User
Jan 21, 2004
38
GB
I have a class Relationships which has various properties. One of which is Person which is a class with various properties.

I am trying to bind my ddl to the property PersonLastName which is a property of Person which is a property of Relationship

So:
ddlPerson.DataTextField = "Person.PersonLastName"
ddlPerson.DataValueField = "Person.PersonID"
ddlPerson.DataSource = _TMSUserSession.RelationshipList
ddlPerson.DataBind()

returns the error _TMSUserSession.RelationshipList does not contain the property Person.PersonLastName

I have a grid view which i bind in mark up using

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" Font-Names="Verdana" Font-Size="X-Small" CellSpacing="2">
<Columns>
<asp:CommandField ButtonType="Image" SelectImageUrl="~/Images/Arrow1.gif"
ShowSelectButton="True" />
<asp:BoundField HeaderText="ID" DataField = "RelationshipID" />

<asp:TemplateField HeaderText="Person Name">
<ItemTemplate>
<asp:Label ID="lblPersonName" runat="server"
Text='<%# Eval("Person").PersonFirstName %>'>
</asp:Label>

<asp:Label ID="lblPersonLastName" runat="server"
Text='<%# Eval("Person").PersonLastName %>'>
</asp:Label>
</ItemTemplate>
</asp:TemplateField>
Etc......

Which works fine but any attempt to use Eval with DataValueField and DataTextField for ddl returns the error

"Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control"

Put simply all I want to do is set my DataTextField and DataValueField properties to properties in a child object. Surely that shouldn't be as hard as it appears!!

Many thanks in advance




 
you need to cast it to the appropriate type
Code:
<%# ((Person)Container.DataItem).PersonLastName %>
or something like that.

another option
Eval("PersonLastName")

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top