且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

的DropDownList"拥有的SelectedValue,因为它不中的项目&QUOT列表存在这是无效的;

更新时间:2022-10-23 18:09:14

这可能是值得先绑定,然后添加你的空白值。沿着线的东西:

  _ddlRequest.Datasource =请求;
_ddlRequest.DataBind();
_ddlRequest.Insert(0,新的ListItem(,));

我认为这个问题的唯一原因是,我碰到这个来之前和敲打我的头在挫折只是定居在做,而不是这样过了一段时间。

I have a ASP.NET DropDownList, which I data-bind to a list of objects, and then it throws an exception. This error is driving me nuts, because the message is wrong, and the value clearly does exist in the list of items, as shown by the screenshot:

Here's the relevant ASP.NET markup:

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

It may be worth first databinding and then adding your blank value. Something along the lines of:

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

The only reason I suggest this is that I came across this before and after banging my head in frustration for a while just settled on doing it this way instead.