SelectedValue 无效,因为它不存在于项目列表中

发布于 2024-08-03 18:45:16 字数 671 浏览 2 评论 0原文

我反复遇到这个问题,但不知道是什么原因造成的。我在 DataBind 中遇到异常:

"SelectedValue which is invalid because it does not exist in the list of items"

以下是一些重要的信息:

  1. 当基础数据发生更改时,我会定期重新加载 listOrgs。
  2. Organization.DTListAll 调用返回大约 500 个 Int、String 对。
  3. 返回的数据中没有重复值或空值
  4. 下面前两行之后,listOrgs.Items.Count 为 0,Selected Value 为 0
  5. DataBind 操作执行时选择的值不在集合中返回的 ID 值

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

I encountered 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. I reload listOrgs periodically when the underlying data has changed.
  2. The Organization.DTListAll call returns about 500 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 when the DataBind operation executes is a value that is not in the set of ID values returned
listOrgs.Items.Clear();
listOrgs.SelectedValue = "0";
listOrgs.DataSource = new Organization().DTListAll(SiteID);
listOrgs.DataTextField = "OrganizationName";
listOrgs.DataValueField = "OrganizationID";
listOrgs.DataBind();

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

别挽留 2024-08-10 18:45:16

检查现有值

    this.DropDownList1.Items.Clear();
    //--dont use this:
    //this.DropDownList1.SelectedValue = "0";
    DataTable dt = new DataTable();
    dt.Columns.Add("x", typeof(System.Int32));
    dt.Columns.Add("xs", typeof(System.String));
    for (int x = 0; x < 100; x++)
    {
        DataRow dr = dt.NewRow();
        dr["x"] = x;
        dr["xs"] = x.ToString();
        dt.Rows.Add(dr);
    }
    DropDownList1.DataValueField = "x";
    DropDownList1.DataSource = dt;
    DropDownList1.DataBind();
    // check for existing value:
    int valueToCheck = 99; // last item
    if (this.DropDownList1.Items.FindByValue(valueToCheck.ToString()) != null)
    {
        this.DropDownList1.SelectedValue = valueToCheck.ToString();
    }

除此之外 - 您可能想在绑定之前尝试设置数据文本和数据值字段(据我所知,这是性能++)

Check for an existing value

    this.DropDownList1.Items.Clear();
    //--dont use this:
    //this.DropDownList1.SelectedValue = "0";
    DataTable dt = new DataTable();
    dt.Columns.Add("x", typeof(System.Int32));
    dt.Columns.Add("xs", typeof(System.String));
    for (int x = 0; x < 100; x++)
    {
        DataRow dr = dt.NewRow();
        dr["x"] = x;
        dr["xs"] = x.ToString();
        dt.Rows.Add(dr);
    }
    DropDownList1.DataValueField = "x";
    DropDownList1.DataSource = dt;
    DropDownList1.DataBind();
    // check for existing value:
    int valueToCheck = 99; // last item
    if (this.DropDownList1.Items.FindByValue(valueToCheck.ToString()) != null)
    {
        this.DropDownList1.SelectedValue = valueToCheck.ToString();
    }

Besides that - you might want to try to set the datatext and datavalue fields before you bind (afaik that is a performance++)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文