I tried this before, and realized that I could not use the attribute attribute DropDownList.
I have done the following:
Create a list containing KeyValuePair. The key in KeyValuePair is the same identifier that you entered in your DropDownList element.
The KeyValuePair value is the value (or values) that you would like to save / connect to your product.
You can save the list in your viewport and read the data after you select an item in your DropDownList and find the correct KeyValuePair using the identifier.
So, you can “save” the data as follows:
var listKeyValuePair = new List<KeyValuePair<int, string>>(); listKeyValuePair.Add(new KeyValuePair<int, string>(1, "data")); ViewState["DataList"] = listKeyValuePair;
And you can get your data as follows:
var listKeyValuePair = (List<KeyValuePair<int, string>>)ViewState["DataList"]; var dataILikeToHave = listKeyValuePair.Find(k => k.Key == Convert.ToInt16(dropDownlist.SelectedItem.Value));
source share