views:

58

answers:

3

Hi,

I'm using DropDownList within my WebApplication. I need to keep additional piece of information with each Item on list. How can I achieve this?

A: 

In HTML a drop down is represented by the <select> element which is a collection of <option>. Each option has a value and text. That's all. So keep the additional information in your data store and use the value of the selected element to query it when needed.

Darin Dimitrov
I need something more than text and value... It's obvious that there are two values. I need third, and that is why I've posted this question.
truthseeker
I don't see the reason for the downvote. You might be looking for a third value, that's OK, but there's no magic. Once you have familiarized yourself with the specification of HTML you will understand why this third value cannot be stored.
Darin Dimitrov
ok, but I would like to hear some idea about possible workaarounds for example using cache. And I don't want to hear obvious things. Please don't assume that the author of the question is rookie or don't know simple things. What is more you could explain how HTML works if you assume that I or any orher person reading this post don't know. Do you know how why I have 140 reputation instead of 141? Revange?
truthseeker
There is a dodgy workaround. The text and value fields of the dropdownlist are just text fields. Therefore you could split the text into two seperate, character delimited fields. Ie DropDownListID.Items.Insert(0, new ListItem("Text", "Value1|Value2");Then you could do .Split('|') to pull out the two values. Dodgy, but its the only workaround.Darin is right though - this is a standard practice for HTML, try not to go against the standards, if this control doesnt suit your scenario then its probably not the right control to use.
RPM1984
@truthseeker, I've already given what I think a workaround might be - keep the value on the server and use the id of the selected value to fetch it. Where you will store it is your choice - Cache, Session, Database, ...
Darin Dimitrov
A: 

How about using your own custom attributes on each of the list items, for example:

<option value="1" data-RecordID="foo">Value 1</option>
<option value="2" data-RecordID="bar">Value 2</option>

Here's a link on how custom attributes will also validate if that is a concern:

HTML 5 data- Attributes

I should also add that the link I included talks about how "data=" attributes are valid XHTML in HTML 5 but there's no reason I can think of to not use them now. You can access them client side using javascript or in .NET server side code using the .Attributes() collection on the .SelectedItem

Rich
A: 

Make a Custom DropDownList, inheriting from the DropDownList of course. Add another property for the data. The easiest implementation would be to have your new property be a collection of the items you are populating the ddl with. You probably have a class that describes the data you already have, so make the property of that type. You could even set the DataTextValue and DataValueField from this collection after it gets populated and not even have to hook that up in the aspx page.

TheGeekYouNeed