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

前端之家收集整理的这篇文章主要介绍了asp.net – SelectedValue是无效的,因为它不存在于项目列表中前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我反复遇到这个问题,并没有一个线索是什么造成它。我在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();

解决方法

@H_301_16@ 显然,我发布的解决方案不是完全有效…最终在我的应用程序,我改为:
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();
原文链接:https://www.f2er.com/aspnet/254406.html

猜你在找的asp.Net相关文章