开发者

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

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:

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

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.


You can use below code to clear data on DropDownList

_ddlRequest.Items.Clear()
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜