2

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?

truthseeker
  • 2,214
  • 6
  • 30
  • 53

4 Answers4

2

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
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • 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 Jul 11 '10 at 21:23
  • 1
    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 Jul 11 '10 at 21:29
  • 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 Jul 11 '10 at 21:54
  • 1
    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 Jul 11 '10 at 23:35
  • @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 Jul 12 '10 at 06:02
1

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
  • 896
  • 6
  • 15
  • This was a great approach to allow this functionality! The gotcha was that I had to drop the `data-` prefix, because I was getting an exception stating that that my property minus the prefix doesn't exist (probably an IE thing). – LostNomad311 Feb 09 '12 at 20:13
0

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
  • 7,509
  • 2
  • 26
  • 43
0

Other options that make your additional data available client-side could be to use a repeater which is bound to the same data source as your drop down and have it render hidden fields with the additional data, or render a javascript array.

Veli Gebrev
  • 2,481
  • 4
  • 31
  • 48