returning entity object from a listbox

Posts   
 
    
jit
User
Posts: 2
Joined: 21-Jan-2005
# Posted on: 21-Jan-2005 13:14:34   

Probably a silly newbie question but after much searching i'm unable to find an answer.

I have a listbox (say listbox A) bound to an enitity collection. I have another listbox (say list box B) bound to another a entity collection.

The user is allowed to remove entities from listbox B if they are present in listbox A. So i need to check whether the users selection in listbox B is present in listbox A.

So i want to do something like

ListboxASourceEntityCollection.indexof(listboxB.selecteditem), but listboxb.selecteditem returns an item not an enitity. I've attempted to cast the item to an entity object but this is not a vaild casting.

So how do i return the selected entity from a listbox?

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39928
Joined: 17-Aug-2003
# Posted on: 21-Jan-2005 14:19:24   

If you bind an entity collection to a listbox, SelectedItem will contain an entityobject, so you should cast to IEntity (selfservicing) or IEntity2 (adapter), or to the entity class, like CustomerEntity.

If you're unsure what the type is, first place it in an object variable and in the debugger inspect its type .

Frans Bouma | Lead developer LLBLGen Pro
jit
User
Posts: 2
Joined: 21-Jan-2005
# Posted on: 21-Jan-2005 14:35:37   

Otis,

I've tried that, declcared an object t and then done t=listbox.selecteditem the type shown for t in the debugger is System.Web.UI.WebControls.ListItem

Here's some code snippets

The binding..

adapter.FetchEntityCollection(selected_user.Locations, selected_user.GetRelationInfoLocations()) lbxUser_locations.DataSource = selected_user.Locations lbxUser_locations.DataTextField = "locationdesc" lbxUser_locations.DataValueField = "locationid" lbxUser_locations.DataBind()

..attempting to get the selected entity

t = lbxUser_locations.SelectedItem

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39928
Joined: 17-Aug-2003
# Posted on: 21-Jan-2005 15:24:38   

ah, then grab: IEntity e = (IEntity)lbxUser_locations.SelectedItem.Value;

Frans Bouma | Lead developer LLBLGen Pro
Posts: 94
Joined: 26-Feb-2006
# Posted on: 10-Jun-2006 11:47:21   

Hey folks,

I am currently struggling with a similar problem... the only difference to the example above is that I use a DropDownList WebControl and the mentioned SelectecItem.Value is a String so casting is not possible...

Basically I have to fill a look up DropDownList with the problem that the PK (nummer) is a composite one so I have to use multiple fields as value for the DropDownList control items. Can I accomplish this using the integrated DataBinding Feature of the EntityCollection or do I have to make a workaround with string concatenation using SQL???

The UMO184 Entity consists of those field:

NUMMER (PK) Integer LOCATIE (PK) Short OMSCHR String


       'load  UMO184NRCollection
            DropDownListUMO184NR.DataSource = LookupTableProvider.Umo184NrCollection()
            DropDownListUMO184NR.DataTextField = "OMSCHR"
            DropDownListUMO184NR.DataValueField = "NUMMER"
            DropDownListUMO184NR.DataBind()

<--- snip  --->

'grab the selected value 

        If (Not DropDownListUMO184NR.SelectedValue.Equals("0")) Then
                UMO138.Umo184Nr = CType(DropDownListUMO184NR.SelectedValue, Integer)
                UMO138.Umo184Loc = 0 ' here is the problem! How do I get the Value from the entity
          Else
                UMO138.SetNewFieldValue(DataAccess.Umo138FieldIndex.Umo184Loc, Nothing)
                UMO138.SetNewFieldValue(DataAccess.Umo138FieldIndex.Umo184Nr, Nothing)
          End If