Since_2008
Since_2008

Reputation: 2341

ASP.NET & C# - Two values in a listbox?

I'm adding ListItems to a ListBox from two controls, both are DropDownLists.

The ListItem has the properties ListItem.SelectedItem and ListItem.SelectedValue, but I also want the ListBox to keep track of which DropDownList the ListItem came from.

What would be the best way to do this?

Upvotes: 2

Views: 586

Answers (5)

Since_2008
Since_2008

Reputation: 2341

The way I went about solving this, was creating an ArrayList, with another array inside for each item.

Upvotes: 0

StefanE
StefanE

Reputation: 7630

You could create an in memory dataset to store all the data needed and then bind the dataset to your ListBox. When ever a value is selected you will have access to more fields in code behind.

Upvotes: 0

Erix
Erix

Reputation: 7105

You could set the ID of the dropdown programatically as a attribute of the listitem.

ListItem i = new ListItem();
i.Attributes["IDofDropDown"] = "SomeID";

Upvotes: 5

Canavar
Canavar

Reputation: 48108

ListItem item contains Text and Value properties that mapped from option element in HTML. So you can not add any other value to this structure. But maybe you can save the source dropdown's id to the Value of the ListItem.

Upvotes: 0

rtpHarry
rtpHarry

Reputation: 13135

Encode the .Value section so that its something like DropDownList1:SomeValue and then you can use String.Split to extract the info back out of .SelectedValue?

Upvotes: 0

Related Questions