How to Search Through a C# DropDownList Programmatically

后端 未结 9 1170
一生所求
一生所求 2021-02-14 05:24

I am having a hard time figuring out how to code a series of \"if\" statements that search through different dropdownlists for a specific value entered in a textbox. I was able

相关标签:
9条回答
  • 2021-02-14 05:37

    One line of code- split for readablilty.

    this.DropDownList1.SelectedItem = this.DropDownList1.Items
         .SingleOrDefault(ddli => ddli.value == this.textbox1.value);
    
    0 讨论(0)
  • 2021-02-14 05:40
    while(dropdownlist1.SelectedIndex++ < dropdownlist1.Items.Count)
    {
        if (dropdownlist1.SelectedValue == textBox1.text)
        {
          // Add your logic here.
        }
    }
    
    //resetting to 0th index(optional)
    dropdownlist1.SelectedIndex = 0;
    
    0 讨论(0)
  • 2021-02-14 05:46

    I would make a list of Drop-down boxes and then use linq to select on it.

    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;
    
    0 讨论(0)
  • 2021-02-14 05:51

    I was trying to find item by text in dropdownlist. I used the code below, it works: )

    ListItem _lstitemTemp = new ListItem("Text To Find In Dropdownlist");
    if(_DropDownList.Items.Contains(_lstitemTemp))
    {
        dosomething();
    }
    
    0 讨论(0)
  • 2021-02-14 05:52

    You can simply do like this.

    if (ddl.Items.FindByValue("value") != null) {
       ddl.SelectedValue = "value";
    };
    
    0 讨论(0)
  • 2021-02-14 05:53

    The DropDownList inherits the Items collection from the ListControl. Since Items is an Array, you can use this syntax:

    dropdownlist1.Items.Contains(textbox1.Text) as a boolean.

    0 讨论(0)
提交回复
热议问题