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

前端 未结 2 1753
暗喜
暗喜 2021-01-19 06:41

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

相关标签:
2条回答
  • 2021-01-19 07:20

    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.

    0 讨论(0)
  • 2021-01-19 07:34

    You can use below code to clear data on DropDownList

    _ddlRequest.Items.Clear()
    
    0 讨论(0)
提交回复
热议问题