A Bug in DropDownList
5486 단어 list
I found there was a bug in System.Web.UI.WebControls.DropDownList recently. When I created items for a DropDownList control using DropDownList.DataBind method, an exception was always thrown. The error message looked like:
'DropDownList_Option' has a SelectedValue which is invalid because it does not exist in the list of items. Parameter name: value
At first, I thought maybe I should clear the selection before invoking DataBind method because the word "SelectedValue"was mentioned in the error. I then began to try the following methods:
But unfortunatelly none of the them worked. The exception was still thrown. I got confused at that time. Because abviously we had nothing selected. With "Reflector"'s help, I finally found the reason. It seemed it was a bug there which I would introduce the details below. To bypass the bug, you can just simply try:
DropDownList_Option.SelectedValue = null;
It was so strange, wasn't it?
How to reproduce the bug
It's very simple to reproduce the bug. Just use the code segment below:
// MyDataItem class definition
//
public class MyDataItem
{
private string _text;
private string _value;
public MyDataItem(string text, string value)
{
_text = text;
_value = value;
}
public string Text
{
get { return _text; }
}
public string Value
{
get { return _value; }
}
}
// Reproduce the DropDownList bug here
// e.g. we have a DropDownList variable called DropDownList_Option for now.
//
List
items1 = new List
(); items1.Add(new MyDataItem("Text1", "Value1")); items1.Add(new MyDataItem("Text2", "Value2")); items1.Add(new MyDataItem("Text3", "Value3")); DropDownList_Option.DataTextField = "Text"; DropDownList_Option.DataValueField = "Value"; DropDownList_Option.DataSource = items1; DropDownList_Option.DataBind(); // Makes the first item selected. // Please note if we use "
DropDownList_Option.SelectedIndex = 0;" here, there will be no exceptions. // DropDownList_Option.SelectedValue = "Value1"; List
items2 = new List
(); items2.Add(new MyDataItem("2 - Text1", "2 - Value1")); items2.Add(new MyDataItem("2 - Text2", "2 - Value2")); items2.Add(new MyDataItem("2 - Text3", "2 - Value3")); DropDownList_Option.SelectedIndex = -1; DropDownList_Option.ClearSelection(); DropDownList_Option.DataSource = items2; DropDownList_Option.DataBind(); // An exception will be thrown here!
Why does the bug appear?
To realise why the bug always appears, we have to have a deep look into .Net Framework source code. We are so lucky that we have Reflector. It provides us the valuable source code. Thanks Reflector! :)
// Generated code by Reflector
if(dataSource != null)
{
// Creates ListItem(s) here
// ....
}
if (this.cachedSelectedValue != null)
{
int num = -1;
num = this.Items.FindByValueInternal(this.cachedSelectedValue, true);
if (-1 == num)
{
throw new ArgumentOutOfRangeException("value", SR.GetString("ListControl_SelectionOutOfRange", new object[] { this.ID, "SelectedValue" }));
}
// ....
}
There are also 2 similar variables called "cachedSelectedIndex/cachedSelectedIndices"in ListControl. I'm not sure whether they'll cause exceptions or not. You can try it if you're interested in it.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
[SwiftUI]List화한 CoreData를 가로 스와이프로 행 삭제하는 방법상당히 조사했지만 일본어 자료가 없었기 때문에 비망록으로 남겨 둔다. 아래와 같이 CoreData를 참조한 리스트를 가로 스와이프로 삭제하고 싶었다. UI 요소뿐만 아니라 원본 데이터 당 삭제합니다. 잘 다른 페이지...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.