3

我正在尝试使用 System.Web.UI.WebControls.ListItems 的 Collections.Generic.List 对 asp:DropDownList 进行 DataBind。DataBind() 抛出此错误。

System.ArgumentOutOfRangeException:“ddlPlatinumOptions”有一个无效的 SelectedValue,因为它不存在于项目列表中。

.ascx

<asp:DropDownList ID="ddlPlatinumOptions" runat="server" AutoPostBack="true" width="100%"></asp:DropDownList>

.ascx.cs

public void BindPlatinumOptions()
{
    ddlPlatinumOptions.DataTextField = "Text";
    ddlPlatinumOptions.DataValueField = "Value";
    ddlPlatinumOptions.DataSource = _platinumOptions;
    ddlPlatinumOptions.DataBind(); // Throwing Error
}

主持人

MattressProtectionInfo standard = RF_ProtectionPlan.GetMattressPlanInfo(MattressId, false);
MattressProtectionInfo premium = RF_ProtectionPlan.GetMattressPlanInfo(MattressId, true);
List<ListItem> plans = new List<ListItem>();                  
if (standard != null)
{
    plans.Add(new ListItem(standard.Price.ToString("C") + " - Standard 5-Year Platinum Protection", standard.ProductID.ToString()));
}
if (premium != null)
{
    plans.Add(new ListItem(premium.Price.ToString("C") + " - Premium 5-Year Platinum Protection", premium.ProductID.ToString()));
}

_view.PlatinumOptions = plans;
_view.BindPlatinumOptions();

数据示例

  • 价值 =“21696”文本 =“99.95 美元 - 标准 5 年白金保护”
  • 价值 =“21702”文本 =“119.95 美元 - 高级 5 年白金保护”

我试过的东西

  • 在我的数据之前清空数据源和数据绑定以清除任何内容(在 dataBind 上也中断)
  • 重新定位 DataTextField 和 DataValueField 的位置(浪费时间 - 没有变化)
  • 在数据绑定之前声明选定的索引为 0
  • ddlPlatinumOptions.Items.Clear();
  • ddlPlatinumOptions.ClearSelection();

我正在抓稻草。似乎数据绑定正在尝试选择下拉列表中不存在的内容。

我的代码中是否有错误我没有看到?有任何想法吗?

4

2 回答 2

5

在遇到这个问题后,我也调试了 ASP.NET 源代码,并在 ASP.NET 源代码中发现了一个缺陷(?)(因此,我们的问题的解决方案):

在内部,System.Web.UI.WebControls.Listcontrol缓存最近使用的SelectedValue. 因此,当在已经设置 from之后发生数据绑定(本机清除SelectedValue属性)时,数据绑定操作会尝试查找并预选先前缓存的项目。不幸的是,当在新列表中找不到缓存的值而不是静默地留下当前值时,它会引发异常。SelectedValueRequest.Form[]DataSourceSelectedValue null

我猜他们这样做是因为在使用ViewState 数据源对象进行数据绑定时,后面的数据绑定操作会删除SelectedValueRequest.Form[].

因此,每当您在页面生命周期内对 a 执行多个数据绑定操作ListControl并且数据源不同时,就会发生此异常。


这是 ASP.NET 代码概要:

    public virtual string SelectedValue
    {
        set
        {
            if (Items.Count != 0)
            {
                // at design time, a binding on SelectedValue will be reset to the default value on OnComponentChanged
                if (value == null || (DesignMode && value.Length == 0))
                {
                    ClearSelection();
                    return;
                    // !!! cachedSelectedValue is not getting reset here !!!
                }

            ...

            // always save the selectedvalue
            // for later databinding in case we have viewstate items or static items
            cachedSelectedValue = value;
        }
    }


这是解决方案:

代替:

    dropDownBox.DataSource = ...;
    dropDownBox.DataBind();

写这个:

    dropDownBox.Items.Clear();
    dropDownBox.SelectedValue = null;
    dropDownBox.DataSource = ...;
    dropDownBox.DataBind();


(仅当它不是空dropDownBox.Items.Clear();的时才需要该操作。)dropDownBox.Items

于 2016-11-22T16:53:42.610 回答
2

好吧,这是一个超出范围的异常。在绑定到 DropDownList 之前,您是否将 SelectedIndex 设置为默认值?

于 2014-12-04T19:42:55.517 回答