ASP.NET Web 窗体 DropDownList 有一个无效的 SelectedVa

ASP.NET Web Forms DropDownList has a SelectedValue which is invalid because it does not exist in the list of items(ASP.NET Web 窗体 DropDownList 有一个无效的 SelectedValue,因为它不存在于项目列表中)
本文介绍了ASP.NET Web 窗体 DropDownList 有一个无效的 SelectedValue,因为它不存在于项目列表中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先有问题(DropDownList 有一个无效的 SelectedValue,因为它不存在于项目列表中,DropDownList有一个无效的 SelectedValue,因为它不存在于项目列表中",asp:DropDownList 错误:'DropDownList1' 有一个 SelectedValue是无效的,因为它不存在于项目列表中 )关于此,并且有建议的解决方法,但我的问题实际上是为什么会发生这种情况.更重要的是,我对建议的解决方法不满意,而且我觉得它们很丑陋.

所以有一个带有下拉列表和按钮的页面:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm2.aspx.cs" Inherits="TestWebApplication.WebForm2" ViewStateMode="Disabled" %><html lang="zh-cn" ><身体><form id="form1" runat="server"><div><asp:DropDownList ID="ddlTest" runat="server"></asp:DropDownList><asp:Button Text="Test" ID="btnTest" runat="server" onclick="btnTest_Click"/>

</表单>

我将 ddlTest 与 Page_Init 上的一些项目绑定,然后在 btnTest_Click 中再次绑定:

使用系统;命名空间 TestWebApplication{公共部分类 WebForm2 : System.Web.UI.Page{protected void Page_Init(对象发送者,EventArgs e){//SelectedIndex 为-1,SelectedValue 为"",SelectedItem 为空ddlTest.DataSource = new[] { 1, 2, 3 };ddlTest.DataBind();ddlTest.SelectedValue = "3";}protected void btnTest_Click(object sender, EventArgs e){//SelectedIndex 为 2,SelectedValue 为3",SelectedItem 为 {3}ddlTest.ClearSelection();//SelectedIndex 为 0,SelectedValue 为1",SelectedItem 为 {1}ddlTest.SelectedIndex = -1;//没有变化,包括SelectedIndexddlTest.SelectedValue = "";//没有任何变化,包括SelectedValueddlTest.Items.Clear();//SelectedIndex 为-1,SelectedValue 为"",SelectedItem 为空ddlTest.DataSource = null;//除了DataSource属性没有任何变化ddlTest.DataSource = new[] { 1, 2 };ddlTest.DataBind();//异常!//'ddlTest' 有一个无效的 SelectedValue,因为它不存在于项目列表中.//参数名称:值}}}

为什么会出现异常.我尝试了这些的不同版本,但没有一个有效.我尝试只使用 ClearSelection,但仍然遇到相同的异常.这是控件中的错误还是我想念的东西.其他问题的丑陋解决方法是唯一的解决方案吗?

注意 - 即使按钮被移除并且所有代码都在单个事件处理程序中移动,该错误也是可重现的.只需绑定一次设置选定的值并再次绑定.

解决方案

我已在 Connect 上针对此问题提交了一个错误.它被解决为无法修复",在我看来这意味着它实际上是一个错误.提供了一种解决方法:

ddlTest.Items.Clear();ddlTest.SelectedValue = null;

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:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm2.aspx.cs" Inherits="TestWebApplication.WebForm2" ViewStateMode="Disabled" %>

<html lang="en" >
<body>
    <form id="form1" runat="server">
    <div>
        <asp:DropDownList ID="ddlTest" runat="server">
        </asp:DropDownList>
        <asp:Button Text="Test" ID="btnTest" runat="server" onclick="btnTest_Click" />
    </div>
    </form>
</body>
</html>

I bind ddlTest with some items on Page_Init and then in btnTest_Click I bind again:

using System;

namespace TestWebApplication
{
    public partial class WebForm2 : System.Web.UI.Page
    {
        protected void Page_Init(object sender, EventArgs e)
        {
            //SelectedIndex is -1, SelectedValue is "", SelectedItem is null
            ddlTest.DataSource = new[] { 1, 2, 3 };
            ddlTest.DataBind();
            ddlTest.SelectedValue = "3";
        }

        protected void btnTest_Click(object sender, EventArgs e)
        {
            //SelectedIndex is 2, SelectedValue is "3", SelectedItem is {3}
            ddlTest.ClearSelection();
            //SelectedIndex is 0, SelectedValue is "1", SelectedItem is {1}
            ddlTest.SelectedIndex = -1; //Nothing changes including SelectedIndex
            ddlTest.SelectedValue = ""; //Nothing changes including SelectedValue
            ddlTest.Items.Clear();
            //SelectedIndex is -1, SelectedValue is "", SelectedItem is null
            ddlTest.DataSource = null; //Nothing changes except for the DataSource property
            ddlTest.DataSource = new[] { 1, 2 };
            ddlTest.DataBind();//Exception!
            //'ddlTest' has a SelectedValue which is invalid because it does not exist in the list of items.
            //Parameter name: value
        }
    }
}

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:

ddlTest.Items.Clear();
ddlTest.SelectedValue = null;

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,因为它不存在于项目列表中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
how do i pass parameters to aspnet reportviewer(如何将参数传递给aspnet report查看器)
Bind multiple parameters from route and body to a model in ASP.NET Core(在ASP.NET Core中将路由和主体中的多个参数绑定到一个模型)
Custom model binding in AspNet Core WebApi?(AspNet Core WebApi中的自定义模型绑定?)
How to minify in .net core mvc view?(如何在.Net核心MVC视图中缩小?)