I am trying to bind a dictionary to the drop-down list.
How to specify “text” (the key of the dictionary is the drop-down text) and “value” (value) for the drop-down list?
Can you please help?
Note: There is a constraint that classes should not be introduced for this purpose. This is why I tried to use a dictionary.
Thank you
Lijo
dropdown.DataValueField = "Key";
dropdown.DataTextField = "Value";
dropdown.DataSource = myDictionary;
dropdown.DataBind();
Since you actually bind KeyValuePair
to each project, the properties you want to access are Key
and Value
.< /p>
I use C#’s drop-down list in ASP.NET.
I am trying to bind a dictionary to the drop-down list.
How to specify “text” (the key of the dictionary is the drop-down text) and “value” (value) for the drop-down list?
Can you please help?
Note: There is a constraint that classes should not be introduced for this purpose. This is why I tried to use a dictionary.
Thank you
Lijo
Set the drop-down list to use keys and values as follows:
dropdown.DataValueField = " Key";
dropdown.DataTextField= "Value";
dropdown.DataSource = myDictionary;
dropdown.DataBind();
Because you will actually KeyValuePair
is bound to each item, so the properties you want to access are Key
and Value
.
p>