首先有一些问题(
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)关于这个问题,并提出了解决方法,但我的问题是真的为什么发生这种情况。更重要的是,我对建议的解决方法不满意,我觉得他们很丑陋。
<%@ 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>
我将ddlTest绑定在Page_Init上的某些项目,然后在btnTest_Click中再次绑定:
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,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 } } }
为什么我得到例外我尝试过不同版本的这些版本,它们都不工作。我尝试仅使用ClearSelection,但仍然有相同的异常。这个错误在控制或我想念的东西。其他问题的难处理解决方案是唯一的解决方案吗?
解决方法
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。