C# System.Web.HttpException:不能在 DropDownList 中选择多个项目

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1603506/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 19:17:19  来源:igfitidea点击:

System.Web.HttpException: Cannot have multiple items selected in a DropDownList

c#asp.netdrop-down-menu

提问by Bill Paetzke

During page load, index 0 was already selected. Then this code statement selected index 1:

在页面加载期间,索引 0 已被选中。然后这个代码语句选择了索引 1:

dropDownList.Items.FindByValue(myValue).Selected = true; 
// assume myValue is found at index 1 of dropDownList.Items

On completion of page load, the page reads: "System.Web.HttpException: Cannot have multiple items selected in a DropDownList."

页面加载完成后,页面显示:“System.Web.HttpException:不能在 DropDownList 中选择多个项目”。

Why did I get the exception? And how can I fix it?

为什么我得到了异常?我该如何解决?

采纳答案by Bill Paetzke

I noticed that both index 0 and index 1 had the properties "Selected" set to true (dropDownList.Items[0].Selected and dropDownList.Items[1].Selected both were true). However, dropDownList.SelectedIndex was still 0, even though index 1 was set most recently.

我注意到索引 0 和索引 1 的属性“Selected”都设置为 true(dropDownList.Items[0].Selected 和 dropDownList.Items[1].Selected 都是 true)。但是, dropDownList.SelectedIndex 仍然为 0,即使最近设置了索引 1。

I tried resolving this by clearing the list selection beforehand.

我尝试通过事先清除列表选择来解决此问题。

dropDownList.ClearSelection();
dropDownList.Items.FindByValue(myValue).Selected = true;

But that didn't help. Same exception occurred.

但这没有帮助。发生了同样的异常。

What did help, was setting the selected value another way:

有什么帮助,是以另一种方式设置选定的值:

dropDownList.SelectedIndex = dropDownList.Items.IndexOf(dropDownList.Items.FindByValue(myValue));

Now the selection change propogates throughout the list.

现在选择更改在整个列表中传播。

So, don't use dropDownList.Items[x].Selected = true/false to change the selected value of a DropDownList. Instead, use dropDownList.SelectedIndex = x;

因此,不要使用 dropDownList.Items[x].Selected = true/false 来更改 DropDownList 的选定值。相反,使用 dropDownList.SelectedIndex = x;

回答by Tom Hamming

I just had this problem, and found out it was caused by something different. I was adding the same ListIteminstance to multiple dropdowns:

我刚刚遇到了这个问题,并发现它是由不同的东西引起的。我将相同的ListItem实例添加到多个下拉列表中:

ListItem item = new ListItem("Foo", "1");
ListItem item2 = new ListItem("Bar", "2");
ddl1.Items.Add(item);
ddl2.Items.Add(item);
ddl1.Items.Add(item2);
ddl2.Items.Add(item2);

Then setting the SelectedValue:

然后设置 SelectedValue:

ddl1.SelectedValue = "1"; //sets the Selected property of item
ddl2.SelectedValue = "2"; //sets the Selected property of item2

Switching to adding separate instances of ListItemfixed the problem.

切换到添加单独的实例ListItem修复了问题。

My guess is that when you set the SelectedValueof the DropDownList, it sets the Selectedproperty on the appropriate ListItemin its Itemscollection. So in this case, at the end of the second code block, both items are selected in both dropdowns.

我的猜测是,当您设置 的SelectedValueDropDownList,它会将Selected属性设置ListItem在其Items集合中的适当位置。所以在这种情况下,在第二个代码块的末尾,两个下拉列表中的两个项目都被选中。

回答by Adams Biyi

I had a similar problem but under a slightly different scenario. I thought I should post it and the resolution here as it may help someone save time if they happen to be in my similar scenario.

我有一个类似的问题,但在稍微不同的情况下。我想我应该在这里发布它和解决方案,因为如果他们碰巧遇到类似的情况,它可能会帮助某人节省时间。

First the error message:

首先是错误信息:

AMError: Sys.WebForms.PageRequestManagerServerErrorException:
Sys.WebForms.PageRequestManagerServerErrorException: 
Cannot have multiple items selected in a DropDownList.

My Scenario:

我的场景:

I was using using VisualStudio 2010 to step through the application (ASP VB Net) when I encountered the problem. I looked over the 2 dropdownlistson the page, checked the internet and wasted several hours w/o any resolution.

遇到问题时,我正在使用 VisualStudio 2010 单步执行应用程序 (ASP VB Net)。我查看了dropdownlists页面上的 2 ,检查了互联网并浪费了几个小时而没有任何解决方案。

Resolution:

解析度:

Then I got feedup and exited VS 2010 and took a break. When I came back. I reran the application and there was no problem. That's when I realized my costly mistake: I had setup an expression that set the SelectedValuein the Debugger Watch Window! Hence the multiplicity!

然后我受够了并退出了 VS 2010 并休息了一下。我回来的时候。我重新运行应用程序,没有问题。那时我意识到我的代价高昂的错误:我设置了一个表达式,SelectedValue在调试器观察窗口中设置了 !因此多样性!

I removed the expression and all was well again --- Visual Studion 2010 able to get past the dropdownlistsection onto the another area of focus of the application.

我删除了表达式,一切又恢复了 --- Visual Studion 2010 能够通过该dropdownlist部分进入应用程序的另一个重点区域。