asp.net - 下拉列表 "has a SelectedValue which is invalid because it does not exist in the list of items"

标签 asp.net data-binding

我有一个 ASP.NET DropDownList,我将它数据绑定(bind)到一个对象列表,然后它抛出一个异常。这个错误让我发疯了,因为消息是错误的,并且该值显然确实存在于项目列表中,如屏幕截图所示:
enter image description here

这是相关的 ASP.NET 标记:

<asp:DropDownList ID="_ddlRequests" runat="server" AppendDataBoundItems="true" DataTextField="RequestNumber" DataValueField="RequestNumber">
    <asp:ListItem Text="" Value="" />
</asp:DropDownList>

最佳答案

可能值得首先进行数据绑定(bind),然后添加您的空白值。类似于以下内容:

_ddlRequest.Datasource = request;
_ddlRequest.DataBind();
_ddlRequest.Insert(0, new ListItem("", ""));

我建议这样做的唯一原因是,我在沮丧地撞了头一段时间之前和之后都遇到了这个问题,只是决定这样做。

关于asp.net - 下拉列表 "has a SelectedValue which is invalid because it does not exist in the list of items",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5704651/

相关文章:

c# - WPF - 设置 IsOpen=true 时工具提示数据绑定(bind)不起作用

c# - 路径可视化

wpf - 依赖属性和数据绑定(bind)?

javascript - 绑定(bind) html 时不会发生 knockout 表单提交

c# - 我可以将 IFormatProvider 与 WPF 绑定(bind)一起使用到自定义类型吗?

asp.net - WebAPI 中的 DTO 和投影

c# - ASP.NET MVC 2 模型子类始终为 null

asp.net - 主题与 ASP.NET 中的 CSS 相比有何优势?

c# - "Exception thrown: ' System.FormatException ' in Microsoft.SqlServer.Types.dll "在 ASP.NET 控制台应用程序 C#

c# - GridView排序和分页