ASP.NET Web窗体DropDownList具有SelectedValue,因为它不存在于项目列表中,因此无效
|
首先有一些问题( 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,但仍然有相同的异常。这个错误在控制或我想念的东西。其他问题的难处理解决方案是唯一的解决方案吗? 注意 – 即使删除按钮并且所有代码都在单个事件处理程序中移动,该错误也是可重现的。只要绑定一次设置所选值并再次绑定。 解决方法我在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 我想这个答案。 (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- asp.net-web-api – 在Web Api / Owin架构中,处理“/ token
- asp.net-mvc-3 – 为什么两个类,视图模型和域模型?
- asp.net-core – 外部包中的模型显示在swagger文档中
- asp.net-mvc – 部署同一应用程序的2个版本
- asp.net-mvc – 发布具有多个部分视图的表单
- asp.net-mvc – 如何获取针对Razor View Engine的IntelliSe
- asp.net – Web发布的密码不同于我的Azure管理员密码?
- asp.net-mvc – 如何在ASP MVC中自定义Html.ValidationMess
- 如何在asp.net核心中使用ViewDataDictionary和Html.Partial
- asp.net-mvc – 验证:Model或ViewModel
- asp.net – 如何在回发中保存asp:HiddenField值
- .net – TagBuilder从MVC 3 beta版转到RC
- 在asp.net中使用cookie mvc c#
- asp.net-web-api – mvc webapi cross domain po
- asp.net-mvc – ASP.NET MVC运行IIS7部署问题
- asp.net – 在数据库中存储SHA512密码哈希
- asp.net-mvc – 从当前访问者获取CultureInfo并基
- asp.net-mvc-4 – 如何防止复杂类型的默认绑定器
- asp.net – $(“#dialog”).parent().appendTo($
- asp.net-mvc – 如何使输入字段仅允许使用EF和数
