我正在尝试使用 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();
我正在抓稻草。似乎数据绑定正在尝试选择下拉列表中不存在的内容。
我的代码中是否有错误我没有看到?有任何想法吗?