如何通过编程方式搜索C#DropDownList

10 c# search drop-down-menu

我很难搞清楚如何编写一系列"if"语句,这些语句通过不同的下拉列表搜索文本框中输入的特定值.我能够编写在每个下拉列表中找到特定值的代码; 但是,在此之前,我需要添加一个"if"语句,"如果dropdownlist不包含特定值,请转到next if语句,依此类推".以下是我到目前为止的一个例子:

if (dropdownlist1.SelectedValue == textbox1)
{
  dropdownlist1.SelectedIndex = dropdownlist1.items.indexof(dorpdownlist1.items.findbyvalue(textbox1.text) ...

if (dropdownlist2.SelectedValue == textbox1)
{
  dropdownlist2.SelectedIndex = dropdownlist2.items.indexof(dorpdownlist2.items.findbyvalue(textbox1.text) ...

etc...
Run Code Online (Sandbox Code Playgroud)

这样做是根据我在textbox1中的条目读取或扫描每个下拉列表中的第一个值或索引.不幸的是,它只识别第一个值或索引.我需要弄清楚如何在每个"if"语句中扫描整个下拉列表中的所有值,以找到匹配的textbox1值.有没有人有什么建议?

谢谢,

DFM

JB *_*ing 22

foreach (ListItem li in dropdownlist1.Items)
{
    if (li.Value == textBox1.text)
    {
       // The value of the option matches the TextBox. Process stuff here.
    }
}
Run Code Online (Sandbox Code Playgroud)

这是我建议如何查看值是否在下拉列表中.


Dre*_*hie 7

DropDownList的继承项目从集合列表控件.由于Items是一个数组,您可以使用以下语法:

dropdownlist1.Items.Contains(textbox1.Text)作为布尔值.