Databinding to ASP.NET Listbox and EntityCollections

Posts   
 
    
REdwards
User
Posts: 3
Joined: 09-Feb-2006
# Posted on: 09-Feb-2006 23:34:05   

I am working with an Entity that is laid out like the following:

User -> UserFacilities -> Facilities

The User has a collection of facilities that (s)he can be assigned to. The UserFacilities entity is just a bridge entity that links distinct users to distinct facilities (M:N relationship). I am trying to bind the list of facilities that a user is assigned to into a Listbox on an ASP.NET 1.1 page and have had little success so far.

Here's the code i am using for the databinding.

        lboUserFacilities.DataSource = _user.UserFacility;
        lboUserFacilities.DataTextField = "_user.UserFacility.Facilities.Description";
        lboUserFacilities.DataValueField="_user.UserFacility.FacilityId";

FacilityID is the primary key value that is being shared between the Facilities and the UserFacility table. Description is the text description of a given facility. I will be adding users to facilities by adding facilities to the FacilityCollection of a given user, but just need to get the databinding for this worked out. All the samples i have seen so far have to do with Windows Forms.

Thanks,

Rob

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 10-Feb-2006 07:43:43   

Just use the Facilities collection as your DataSource, as follows:

lboUserFacilities.DataSource = _user.UserFacility.Facilities;
lboUserFacilities.DataTextField = "Description";
lboUserFacilities.DataValueField="FacilityId"; // or "Id" whatever is the correct name inside the FacilityEntity