SelectedValue无效,因为它不存在于项目列表中 [英] SelectedValue which is invalid because it does not exist in the list of items

查看:148
本文介绍了SelectedValue无效,因为它不存在于项目列表中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我多次遇到这个问题,并不知道是什么原因造成的。我在DataBind中收到一个例外: SelectedValue,因为它不存在于项目列表中。

I encounter this problem repeatedly, and haven't a clue what is causing it. I get an exception in the DataBind: SelectedValue which is invalid because it does not exist in the list of items.

这里有一些重要的信息:

Here are some important pieces of information:


  1. 当底层数据发生变化时,我会定期重新加载listOrgs。

  2. Organization.DTListAll调用返回2个Int,String对。

  3. 返回数据中没有重复或空值

  4. 在下面的前两行之后,listOrgs.Items.Count为0,所选值为0

  5. DataBind操作执行后所选值是数据中第一行的ID值

  6. 此异常发生在新的页面加载后第一次执行此代码

  1. I reload listOrgs periodically when the underlying data has changed.
  2. The Organization.DTListAll call returns 2 Int, String pairs.
  3. There are no duplicate or null values in the returned data
  4. After the first two lines below, listOrgs.Items.Count is 0, and the Selected Value is 0
  5. The selected value after the DataBind operation executes is the ID value from the first row in the data
  6. This exception happens the very first time this code is executed after a fresh page load





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


推荐答案

显然,我发布的解决方案并不完全有效。 ..最后在我的申请中,我改为:

Apparently the solution I posted wasn't entirely effective... Eventually in my application I changed to this:

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();

这篇关于SelectedValue无效,因为它不存在于项目列表中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆