检查的最佳方式,如果一个下拉列表中包含的价值? [英] Best way to check if a drop down list contains a value?

查看:125
本文介绍了检查的最佳方式,如果一个下拉列表中包含的价值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当用户导航到一个新的页面,这DDL的选择指数是由一个cookie确定,但如果DDL不包含cookie的值,那么我想它被设置为0。用什么方法将我用于DDL?是循环的最佳方式,或者是有一个简单的if语句我能执行?

这是我所尝试,但它不返回一个bool。

 如果(!ddlCustomerNumber.Items.FindByText(GetCustomerNumberCookie()的ToString()))
    ddlCustomerNumber.SelectedIndex = 0;


解决方案

有两种方法浮现在脑海中:

您可以使用包含像这样:

 如果(ddlCustomerNumber.Items.Contains(新
    列表项(GetCustomerNumberCookie()的ToString())))
{
    // ... code在这里
}

或修改当前的策略:

 如果(ddlCustomerNumber.Items.FindByText(
    GetCustomerNumberCookie()的ToString())!= NULL)
{
    // ... code在这里
}

编辑:还有一个 DropDownList.Items.FindByValue 除了将搜索基于价值观的工作方式FindByText一样的,而不是

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?

This is what I've attempted, but it doesn't return a bool.

if ( !ddlCustomerNumber.Items.FindByText( GetCustomerNumberCookie().ToString() ) )
    ddlCustomerNumber.SelectedIndex = 0;

解决方案

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.

这篇关于检查的最佳方式,如果一个下拉列表中包含的价值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆