views:

255

answers:

2

See also: http://stackoverflow.com/questions/428001/dropdowns-filled-with-same-list-item

After a day of tracing and debugging, I finally figured out that populating 3 DropDownLists with the same collection of items causes the last selected item to appear in all 3 lists.

This appears to be due to the 'selectedness' of an item being a property of the item, rather than a property of the list.

However, this appears to be the case only when an item is selected programmatically.

edit #2: As everyone seems to be answering the wrong question: The following is what is confusing me!

The application appears to work as intended when the user selects items via the control. -- Selecting 3 different items in the Web interface results in the correct 3 different items being entered in the DB.

Can anyone explain why this is the case?

EDIT: The question I am asking is why does it work at all in the browser?

A: 

This has to do with BindingContext and the collection. You need to make copies of the collection and bind to those. I think this was done to make mother-child views easier, where you can get the correct child element, based on the selected mother element.

If your collection is a DataTable, binding a dropdownlist to it will use it's DefaultView. To avoid this, package the table in dataviews;

obj.DataSource = new DataView(dataTable, "", "", DataViewRowState.CurrentRows);

Thies
+1  A: 

It's because you're adding the same ListItem to two different DropDownList controls. The object that is being displayed in each of them is the same so changing the value of that object will be reflected in both DropDownLists.

Think of the dropdowns as being just a way of looking at a collection of objects. You poking the dropdown and telling it to change it's selected value really just results in it looping through the items in it's collection and changing their select value. If you use the same collection of objects for both drop downs, changing the values in the collection will result in both drop downs seeing the same change.

It's like having two windows in a house that both look out on the same dog house. If you were to tell a person looking out of window A to go and paint the dog house blue, even though you didn't tell the person looking out window B to paint "his" dog house, he's still going to see a blue dog house.

Jason