public virtual ListItem SelectedItem { get { int selectedIndex = this.SelectedIndex; if (selectedIndex >= 0) { return this.Items[selectedIndex]; } return null; }}public virtual string SelectedValue { get { int selectedIndex = this.SelectedIndex; if (selectedIndex >= 0) { return this.Items[selectedIndex].Value; } return string.Empty; }}
在沒有選定任何項的情況下,SelectedValue默認(rèn)值是string.Empty,而SelectedItem默認(rèn)值是null(也就是說通過SelectedItem.Value可能發(fā)生異常)因此需要設(shè)定默認(rèn)值:添加items.add("未選定",0,) 避免異常??!
1. selectedIndex——指的是dropdownlist中選項的索引,為int,從0開始,可讀可寫
2. selectedItem——指的是選中的dropdownlist中選項,為ListItem,只讀不寫
3. selectedValue——指的是選中的dropdownlist中選項的值,為string, 只讀不寫
4. selectedItem.Text——指的是選中的dropdownlist中選項的文本內(nèi)容,與selectedItems的值一樣為string,可讀可寫
5. selectedItem.value——指的是選中的dropdownlist中選項的值,與selectedValue的值一樣,為string,可讀可寫
光看文字可能不太理解,我也是通過程序來加深理解的,下面舉個例子: