ASP.NET Web 窗体 DropDownList 有一个无效的 SelectedVa
问题描述
首先有问题(DropDownList 有一个无效的 SelectedValue,因为它不存在于项目列表中,DropDownList有一个无效的 SelectedValue,因为它不存在于项目列表中",asp:DropDownList 错误:'DropDownList1' 有一个 SelectedValue是无效的,因为它不存在于项目列表中 )关于此,并且有建议的解决方法,但我的问题实际上是为什么会发生这种情况.更重要的是,我对建议的解决方法不满意,而且我觉得它们很丑陋.
所以有一个带有下拉列表和按钮的页面:
</表单>
我将 ddlTest 与 Page_Init 上的一些项目绑定,然后在 btnTest_Click 中再次绑定:
为什么会出现异常.我尝试了这些的不同版本,但没有一个有效.我尝试只使用 ClearSelection,但仍然遇到相同的异常.这是控件中的错误还是我想念的东西.其他问题的丑陋解决方法是唯一的解决方案吗?
注意 - 即使按钮被移除并且所有代码都在单个事件处理程序中移动,该错误也是可重现的.只需绑定一次设置选定的值并再次绑定.
我已在 Connect 上针对此问题提交了一个错误.它被解决为无法修复",在我看来这意味着它实际上是一个错误.提供了一种解决方法:
https://connect.microsoft.com/VisualStudio/feedback/details/666808/asp-net-dropdownlist-selectedvalue-is-persisted-which-results-in-exception-if-the-control-is-databound-second-time
我认为这是答案.
First of all there has been questions ( DropDownList has a SelectedValue which is invalid because it does not exist in the list of items , DropDownList "has a SelectedValue which is invalid because it does not exist in the list of items" , asp:DropDownList Error: 'DropDownList1' has a SelectedValue which is invalid because it does not exist in the list of items ) about this and there are proposed workarounds but my question is really WHY this happens. What is more I am not satisfied with the suggested workarounds and I find them quite ugly.
So there is a page with a drop down list and a button:
I bind ddlTest with some items on Page_Init and then in btnTest_Click I bind again:
Why do I get the exception. I tried different versions of these and none of them works. I tried using only ClearSelection but still got the same exception. Is this bug in the control or something I miss. Are the ugly workarounds from the other questions the only solution?
Note - the bug is reproduceable even if the button is removed and the all the code is moved in a single event handler. Just bind once set selected value and bind again.
I've submitted a bug on Connect for the issue. It was resolved as "Won't fix" which in my mind means it is in fact a bug. A workaround was provided:
https://connect.microsoft.com/VisualStudio/feedback/details/666808/asp-net-dropdownlist-selectedvalue-is-persisted-which-results-in-exception-if-the-control-is-databound-second-time
I kind of consider this the answer.
这篇关于ASP.NET Web 窗体 DropDownList 有一个无效的 SelectedValue,因为它不存在于项目列表中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!