2018年9月13日 星期四

DropDownList 一直跳回某個選項(亂跳)


今天發生一個很弔詭的狀況
就是在沒有指定SelectIndex、也沒有重新繫結DropDownList的狀況下
DropDownList 一直跳回某個選項

後來發現原來是因為ListItemValue重複的關係


例如:
DropDownList1. Items.Add(new ListItem("A","1"));
DropDownList1. Items.Add(new ListItem("B","2"));
DropDownList1. Items.Add(new ListItem("C","2"));
DropDownList1. Items.Add(new ListItem("D","2"));
DropDownList1. Items.Add(new ListItem("E","3"));

這時候選 B C D 都會跳回 B
所以要改成下面這樣
Value成為唯一值

DropDownList1. Items.Add(new ListItem("A","A1"));
DropDownList1. Items.Add(new ListItem("B","B2"));
DropDownList1. Items.Add(new ListItem("C","C2"));
DropDownList1. Items.Add(new ListItem("D","D2"));
DropDownList1. Items.Add(new ListItem("E","E3"));

這樣的缺點就是讀取Value時要再拆字串


老實說...
明明就是不同 ListItem 為啥會搞成這樣啊(傻眼)
只能說這就是人森哪....
為了 User 的需求我們都在挑戰 VS 的極限()


沒有留言:

張貼留言