如何通过C#编程程序搜索DropDownList

12

我正在努力想出如何编写一系列“if”语句,以搜索不同的下拉列表中输入文本框中的特定值。我已经能够编写代码,在每个下拉列表中找到特定的值;但是,在此之前,我需要添加一个“if”语句,即“如果下拉列表不包含特定的值,则继续执行下一个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...
这段代码的作用是读取或扫描每个下拉列表中的第一个值或索引,根据我在文本框1中的输入。 不幸的是,它只能识别第一个值或索引。 我需要找出如何通过每个“if”语句扫描整个下拉列表以查找匹配的textbox1值的所有值。 有没有人有什么建议? 谢谢,DFM
9个回答

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

这是我建议的方法来判断下拉列表中是否存在该值。


非常感谢!这节省了我很多时间。 - Kings
如果我想把它们放在一个数组中怎么办? - SearchForKnowledge
1
@SearchForKnowledge,将什么放入数组中?在“if”语句内部,可以轻松地将下拉选项放入数组中。 - JB King
下拉列表中的每个选项。实际上我最终使用了List。感谢回复。 - SearchForKnowledge
我有两个列表。我想为下拉列表中的每个项目添加一个列表中所选项目的值和另一个列表中所选项目。我该怎么做? - Si8
1
@SiKni8,你可以从每个列表中选择所需的项目,并将其添加到下拉列表中。否则,循环遍历每个列表可能是一种粗暴的方法。如果这两种方法都不行,请发布一个新问题以获得更好的结果,而不是劫持我六年前给出的答案。 - JB King

7

DropDownList 继承了 ListControlItems 集合。由于 Items 是一个数组,因此您可以使用以下语法:

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


谢谢回复 - 我以为 "items.contains" 只适用于 ListBoxes。我会研究一下这个。 - DFM

3
您可以简单地按照以下方式进行操作。
if (ddl.Items.FindByValue("value") != null) {
   ddl.SelectedValue = "value";
};

2
我会制作一个下拉框列表,然后使用Linq进行选择。
List<DropDownList> list = new List<DropDownList>();
list.Item.Add(dropdown1);
list.Item.Add(dropdown2); 
.... (etc)

var selected = from item in list.Cast<DropDownList>()
               where item.value == textBox1.text
               select item;

感谢您的回复 - 目前我的技能水平还不够自信/熟悉LINQ。 - DFM

2

如果您想在已加载的组合框中搜索确切值,所提供的解决方案可行。

这种解决方案也可以搜索部分值。它使用搜索按钮和下拉框的文本部分作为搜索条件。

Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearch.Click

    ' query the dropdown object
    ''''''''''''
    ' part 9457 is a "contains" sample
    ' part 111 is a "startswith" sample

    Dim query As IEnumerable(Of [Object]) = _
    From item In cboParts.Items _
    Where (item.ToString().ToUpper().StartsWith(cboParts.Text.ToUpper())) _
    Select (item)

    ' show seached item as selected item
    cboParts.SelectedItem = query.FirstOrDefault()

    ' "startswith" fails, so look for "contains" 
    If String.IsNullOrEmpty(cboParts.SelectedItem) Then

        Dim query1 As IEnumerable(Of [Object]) = _
        From item In cboParts.Items _
        Where (item.ToString().ToUpper().Contains(cboParts.Text.ToUpper())) _
        Select (item)

        ' show seached item as selected item
        cboParts.SelectedItem = query1.FirstOrDefault()

        If String.IsNullOrEmpty(cboParts.SelectedItem) Then
            MsgBox("Part is not in dropdown list")
        End If

    End If

1

我试图在下拉列表中通过文本查找项目。我使用了以下代码,它可以正常工作:)

ListItem _lstitemTemp = new ListItem("Text To Find In Dropdownlist");
if(_DropDownList.Items.Contains(_lstitemTemp))
{
    dosomething();
}

0
一行代码- 为可读性而拆分。
this.DropDownList1.SelectedItem = this.DropDownList1.Items
     .SingleOrDefault(ddli => ddli.value == this.textbox1.value);

0

如果您不想使用LINQ:

        List<ComboBox> dropDowns = new List<ComboBox>();
        dropDowns.Add(comboBox1);
        dropDowns.Add(comboBox2);

        bool found = false;
        ComboBox foundInCombo = null;
        int foundIndex = -1;

        for (int i = 0; i < dropDowns.Count && found == false; i++)
        {
            for (int j = 0; j < dropDowns[i].Items.Count && found == false; j++)
            {
                if (item == textBox1.Text)
                {
                    found = true;
                    foundInCombo = dropDowns[i];
                    foundIndex = j;
                }
            }
        }

0
while(dropdownlist1.SelectedIndex++ < dropdownlist1.Items.Count)
{
    if (dropdownlist1.SelectedValue == textBox1.text)
    {
      // Add your logic here.
    }
}

//resetting to 0th index(optional)
dropdownlist1.SelectedIndex = 0;

1
尽管这段代码可能解决问题,但一个好的答案应该解释代码做了什么以及它如何帮助解决问题。 - BDL

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接