C# 检查下拉列表是否包含值的最佳方法?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2007203/
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
Best way to check if a drop down list contains a value?
提问by Justen
When the user navigates to a new page, this ddl's selected index is determined by a cookie, but if the ddl doesn't contain that cookie's value, then I'd like it to be set the 0. What method would I use for the ddl? Is a loop the best way, or is there a simply if statement I can perform?
当用户导航到新页面时,此 ddl 的选定索引由 cookie 确定,但如果 ddl 不包含该 cookie 的值,则我希望将其设置为 0。我将使用什么方法ddl?循环是最好的方法,还是我可以执行一个简单的 if 语句?
This is what I've attempted, but it doesn't return a bool.
这是我尝试过的,但它不返回布尔值。
if ( !ddlCustomerNumber.Items.FindByText( GetCustomerNumberCookie().ToString() ) )
ddlCustomerNumber.SelectedIndex = 0;
采纳答案by Scott Anderson
There are two methods that come to mind:
有两种方法可以想到:
You could use Contains like so:
您可以像这样使用包含:
if (ddlCustomerNumber.Items.Contains(new
ListItem(GetCustomerNumberCookie().ToString())))
{
// ... code here
}
or modifying your current strategy:
或修改您当前的策略:
if (ddlCustomerNumber.Items.FindByText(
GetCustomerNumberCookie().ToString()) != null)
{
// ... code here
}
EDIT: There's also a DropDownList.Items.FindByValue
that works the same way as FindByText, except it searches based on values instead.
编辑:还有一个DropDownList.Items.FindByValue
与 FindByText 的工作方式相同,除了它基于值进行搜索。
回答by Nathan Wheeler
That will return an item. Simply change to:
这将返回一个项目。只需更改为:
if (ddlCustomerNumber.Items.FindByText( GetCustomerNumberCookie().ToString()) != null)
ddlCustomerNumber.SelectedIndex = 0;
回答by Andy Rose
You could try checking to see if this method returns a null:
您可以尝试检查此方法是否返回空值:
if (ddlCustomerNumber.Items.FindByText(GetCustomerNumberCookie().ToString()) != null)
ddlCustomerNumber.SelectedIndex = 0;
回答by Rubens Farias
What about this:
那这个呢:
ListItem match = ddlCustomerNumber.Items.FindByText(
GetCustomerNumberCookie().ToString());
if (match == null)
ddlCustomerNumber.SelectedIndex = 0;
//else
// match.Selected = true; // you'll probably select that cookie value
回答by Nathan
On C# this works:
在 C# 上,这有效:
if (DDLAlmacen.Items.Count > 0)
{
if (DDLAlmacen.Items.FindByValue("AlmacenDefectoAndes").Value == "AlmacenDefectoAndes")
{
DDLAlmacen.SelectedValue = "AlmacenDefectoAndes";
}
}
Update:
更新:
Translating the code above to Visual Basic doesn't work. It throws "System.NullReferenceException: Object reference not set to an instance of an object.."
将上面的代码转换为 Visual Basic 不起作用。它抛出“System.NullReferenceException:未将对象引用设置为对象的实例..”
So. for this to work on Visual Basic, I had to change the code like this:
所以。为了在 Visual Basic 上工作,我不得不像这样更改代码:
If DDLAlmacen.Items.Count > 0 Then
If DDLAlmacen.Items.Contains(New ListItem("AlmacenDefectoAndes")) Then
DDLAlmacen.SelectedValue = "AlmacenDefectoAndes"
End If
End If
回答by Tobias
If 0 is your default value, you can just use a simple assignment:
如果 0 是你的默认值,你可以只使用一个简单的赋值:
ddlCustomerNumber.SelectedValue = GetCustomerNumberCookie().ToString();
This automatically selects the proper list item, if the DDL contains the value of the cookie. If it doesn't contain it, this call won't change the selection, so it stays at the default selection. If the latter one is the same as value 0, then it's the perfect solution for you.
如果 DDL 包含 cookie 的值,这将自动选择正确的列表项。如果它不包含它,这个调用不会改变选择,所以它保持默认选择。如果后者与值 0 相同,则它是您的完美解决方案。
I use this mechanism quite a lot and find it very handy.
我经常使用这种机制,并发现它非常方便。
回答by Alejandro Haro
//you can use the ? operator instead of if
//你可以使用? 运算符而不是 if
ddlCustomerNumber.SelectedValue = ddlType.Items.FindByValue(GetCustomerNumberCookie().ToString()) != null ? GetCustomerNumberCookie().ToString() : "0";
回答by Vijaya Krishna Tummala
ListItem item = ddlComputedliat1.Items.FindByText("Amt D");
if (item == null) {
ddlComputedliat1.Items.Insert(1, lblnewamountamt.Text);
}
回答by Mia Zhang
If the function return Nothing, you can try this below
如果函数返回Nothing,你可以在下面试试这个
if (ddlCustomerNumber.Items.FindByText(
GetCustomerNumberCookie().ToString()) != Nothing)
{
...
}
回答by bestinamir
Sometimes the value needs to be trimmed of whitespace or it won't be matched, in such case this additional step can be used (source):
有时需要修剪该值的空格,否则将无法匹配,在这种情况下,可以使用此附加步骤(源):
if(((DropDownList) myControl1).Items.Cast<ListItem>().Select(i => i.Value.Trim() == ctrl.value.Trim()).FirstOrDefault() != null){}