2

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?

5 Answers 5

5

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

ListItem i = new ListItem();
i.Attributes["IDofDropDown"] = "SomeID";
Sign up to request clarification or add additional context in comments.

3 Comments

I'm not sure, are these attributes transferred between postbacks ?
Wouldn't this emit it in the html markup and make the code invalid?
@SP, I tried what you suggested via lstTrainers.Items[number].Attributes["TrainerType"] = "Trainer"; lstTrainers.Items[number].Attributes.Add("TrainerType", ExTrainer"); But I'm getting an attribute count of 0 when I look at the ListItem. I'm passing the ListBox to a different object.
0

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?

Comments

0

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.

Comments

0

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.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.