Asp.net
從 DropdownList SelectedItem 獲取屬性
我有一個下拉列表,我需要在其中儲存比標準列表項允許的更多的數據。我採用的方法是為每個列表項添加一個屬性。
我監視更改並可以返回 SelectedIndex,但我不確定如何從那裡取回屬性,或者是否有更簡單的方法來實現這一點。
有任何想法嗎?
嘗試這個:
ddl.SelectedItem.Attributes["key"];
我之前確實嘗試過一次,但我認為我無法真正使用 DropDownList 屬性上的屬性。
我所做的是以下內容:
創建一個包含KeyValuePair的**列表。KeyValuePair 中的鍵與您放入 DropDownList 項中的 ID 相同。
KeyValuePair 的值是您希望與您的項目保持/連接的值(或多個值)。
一旦您在 DropDownList 中選擇了一個項目並使用 ID 找到正確的 KeyValuePair,您就可以將 List 儲存在您的 viewState 中並讀取數據。
所以你可以像這樣“儲存”數據:
var listKeyValuePair = new List<KeyValuePair<int, string>>(); listKeyValuePair.Add(new KeyValuePair<int, string>(1, "data")); ViewState["DataList"] = listKeyValuePair;你可以像這樣得到你的數據:
var listKeyValuePair = (List<KeyValuePair<int, string>>)ViewState["DataList"]; var dataILikeToHave = listKeyValuePair.Find(k => k.Key == Convert.ToInt16(dropDownlist.SelectedItem.Value));