微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

c# – System.Web.HttpException:不能在DropDownList中选择多个项目

页面加载期间,已经选择了索引0.然后这个代码语句选择索引1:
dropDownList.Items.FindByValue(myValue).Selected = true; 
// assume myValue is found at index 1 of dropDownList.Items

页面加载完成后,页面显示为:“System.Web.HttpException:DropDownList中不能选择多个项目”.

为什么我得到例外?我该怎么解决

解决方法

我注意到索引0和索引1的属性“Selected”设置为true(dropDownList.Items [0] .Selected和dropDownList.Items [1] .Selected both are true).但是,dropDownList.Selectedindex仍然为0,即使最近设置了索引1.

我尝试通过事先清除列表选项来解决这个问题.

dropDownList.ClearSelection();
dropDownList.Items.FindByValue(myValue).Selected = true;

但这没有帮助.发生同样的例外

有什么帮助,另一种方式是设置选定的值:

dropDownList.Selectedindex = dropDownList.Items.IndexOf(dropDownList.Items.FindByValue(myValue));

现在的选择变化在整个列表中发生变化.

所以,不要使用dropDownList.Items [x] .Selected = true / false来更改DropDownList的选定值.而是使用dropDownList.Selectedindex = x;

原文地址:https://www.jb51.cc/csharp/96404.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐