ASp.NET Dropdown and Dictionary

I am using a dropdown in ASP.NET with C #.

I am trying to associate a dictionary with a drop down list.

How can we specify β€œText” (dictionary key as β€œReset Text”) and β€œValue” (value as Value) for the drop-down list?

Could you help me?

Note. There is a restriction that a class should not introduce for this purpose. That is why I am trying to use a dictionary.

thank

Lijo

+3
source share
2 answers

Configure the drop-down menu to use Keyand Value, for example:

dropdown.DataValueField = "Key";
dropdown.DataTextField= "Value";
dropdown.DataSource = myDictionary;
dropdown.DataBind();

Since you are actually binding KeyValuePairto each element, the properties you want to access are Keyand Value.

+6
Dictionary<string, string> myDictionary = new Dictionary<string, string>();
myDictionary.Add("1", "Item1");
myDictionary.Add("2", "Item2");
myDictionary.Add("3", "Item3");

DropDownId.DataSource = myDictionary;
DropDownId.DataTextField = "Key";
DropDownId.DataValueField = "Value";
DropDownId.DataBind();

, List<myClass> , ? :

DropDownID.Items.Add(new ListItem("Item4","4"));
+1

Source: https://habr.com/ru/post/1743634/


All Articles