DropDownList的两个属性的区别。

来源:百度知道 编辑:UC知道 时间:2024/05/24 12:40:35
DropDownList.SelectedValue与DropDownList.SelectedItem.Value的区别是什么?

谢谢!

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默认值是string.Empty,而SelectedItem默认值是null(通过SelectedItem.Value可能发生异常)

DropDownList.SelectedValue
The value of the selected item in the list control. The default is an empty string ("").

DropDownList.SelectedItem.Value
A ListItem that represents the lowest indexed item selected from the list control. The default is nullNothingnullptra null reference (Nothing in Visual Basic).

就值来说应该是