加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net – SelectedValue是无效的,因为它不存在于项目列表中

发布时间:2020-12-15 19:08:30 所属栏目:asp.Net 来源:网络整理
导读:我反复遇到这个问题,并没有一个线索是什么造成它。我在DataBind中得到一个异常:SelectedValue,它是无效的,因为它不存在于项目列表中。 这里有一些重要的信息: 当底层数据改变时,我定期重新加载listOrgs。 Organization.DTListAll调用返回2个Int,Strin
我反复遇到这个问题,并没有一个线索是什么造成它。我在DataBind中得到一个异常:SelectedValue,它是无效的,因为它不存在于项目列表中。

这里有一些重要的信息:

>当底层数据改变时,我定期重新加载listOrgs。
> Organization.DTListAll调用返回2个Int,String对。
>返回的数据中没有重复或空值
>在下面的前两行之后,listOrgs.Items.Count为0,所选值为0
> DataBind操作执行后选择的值是数据中第一行的ID值
>这个异常发生在第一次这个代码在新的页面加载后执行

listOrgs.Items.Clear(); 
listOrgs.SelectedValue = "0"; 
listOrgs.DataSource = new Organization().DTListAll(SiteID); 
listOrgs.DataTextField = "OrganizationName"; 
listOrgs.DataValueField = "OrganizationID"; 
listOrgs.DataBind();

解决方法

显然,我发布的解决方案不是完全有效…最终在我的应用程序,我改为:
listOrgs.Items.Clear();
listOrgs.SelectedIndex = -1;
listOrgs.SelectedValue = null;
listOrgs.ClearSelection();     // Clears the selection to avoid the exception (only one of these should be enough but in my application I needed all..)
listOrgs.DataSource = new Organization().DTListAll(SiteID);
listOrgs.DataTextField = "OrganizationName"; 
listOrgs.DataValueField = "OrganizationID"; 
listOrgs.DataBind();

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读