2

I am using dropdownlist in asp.net, it has collection of ListItem that represents the items of the dropdownlist, each ListItem has only two fields to hold the data, Value and Text fields, but those are not enough I would like to hold more data for each item. Let's say Text1 and Text2 in additional fields, but at the same time I would like to preserve the same behavior of the DataBind() method of the dropdownlist.

Stedy
  • 7,359
  • 14
  • 57
  • 77
Sisyphus
  • 900
  • 12
  • 32
  • what exactly you want to do???? – The Hungry Dictator Nov 19 '13 at 07:34
  • i have a class say called Nationality, all the nationalities should be populated to a dropdownlist for the user to choose the nationality, normally people will set nationalityId to Value and nationalityName to Text, but this basic configuration is not enough for my case, i have more values to save, i have NationalityId1, NationalityId2, NationalityName, NationalityActive, only NationalityName should be displayed, and i use all the rest internally, when i call DataBind() i want the 4 values to be set to ListItems collection to read them later using SelectedItem property. – Sisyphus Nov 19 '13 at 08:54

2 Answers2

3

Sure, you can use the Attributes collection:

ListItem li = new ListItem("myText", "myID");
li.Attributes.Add("data-x", "xx");
dropdown.Items.Add(li);

This will give you this HTML:

<select name="dropdown" id="dropdown">
   <option value="myID" data-x="xx">myText</option>
</select>

I suggest you prefix your custom attributes with "data-": http://html5doctor.com/html5-custom-data-attributes/

matk
  • 1,528
  • 2
  • 14
  • 25
  • 1
    i am populating the DropDownList using ddl.DataSource = List; ddl.DataBind(); , is there some way to automatically assign a certain property of ClassA to certain attribute when i call the DataBind method, like the case with setting Value and Text properties of the ListItem? – Sisyphus Nov 19 '13 at 08:56
  • I don't think this is easily doable using the databinding mechanism only. You can't even override and extend the ListItem class with custom attributes because System.Web.UI.WebControls.ListItem is a sealed type. A possibility is to handle the dropdown.DataBound event and add the custom attributes after the standard databinding. – matk Nov 19 '13 at 09:13
  • 3
    also note that those custom attributes won't survive postback because they are not added to ViewState. – matk Nov 19 '13 at 09:15
  • yeah, that's what i realised when i tried to extend the dropdownlist, that ListItem is sealed. if custom attributes doesnt survive postbacks, then unfortunately this makes this solution not suitable, beccause i have to keep them through postbacks, unless i am willing to populate them from the database on every postback, which is too much traffic. any suggestion to make them survive postbacks? – Sisyphus Nov 19 '13 at 09:21
  • check out this thread: http://stackoverflow.com/questions/1313447/listitems-attributes-in-a-dropdownlist-are-lost-on-postback – matk Nov 19 '13 at 09:28
  • ...or, of course, if the database hit is a concern, you can always keep the information around in the session or a cache. – matk Nov 19 '13 at 09:34
  • i guess session would be the best available option, thank you for the help mate :) – Sisyphus Nov 19 '13 at 10:12
0

If you want to be able to preserve the items, try setting the AppendDataBoundItems property to true. Then you can programmatically add more items:

dropDown.Items.Add("Another Item", "2");

and preserve the original ones.

Hanlet Escaño
  • 17,114
  • 8
  • 52
  • 75