DropDownList1.SelectedIndex = 1;
DropDownList1.SelectedIndex = 2;上边两句只有第2句是有效的,当最后一次设置SelectedIndex值时会自动先将其他选择项取消,再重新设置选择项,所以上边语句是无错的,最终索引为2的项被选中!但使用Selected属性来设置选择项就没有那么幸运了:
DropDownList1.Items[1].Selected = true;
DropDownList1.Items[2].Selected = true;程序出错:System.Web.HttpException: 不能在 DropDownList 中选择多个项。原因是通过Selected设置选择项时并不会自动先将其他选择项取消,这样会造成有多项处于选中状态,这是不允许的!所以在使用Selected来设置选择项时,在有可能会有多项被选中时应手动取消全部选择项,方法也很简单,如下边例子:
DropDownList1.SelectedIndex = -1;//取消全部选择
//DropDownList1.Items[DropDownList1.SelectedIndex].Selected = false;//取消全部选择
DropDownList1.Items[1].Selected = true;
DropDownList1.SelectedIndex = -1;//取消全部选择
//DropDownList1.Items[DropDownList1.SelectedIndex].Selected = false;//取消全部选择
DropDownList1.Items[2].Selected = true;
评论(0)
暂无评论。