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
One line of code- split for readablilty.
this.DropDownList1.SelectedItem = this.DropDownList1.Items
.SingleOrDefault(ddli => ddli.value == this.textbox1.value);
while(dropdownlist1.SelectedIndex++ < dropdownlist1.Items.Count)
{
if (dropdownlist1.SelectedValue == textBox1.text)
{
// Add your logic here.
}
}
//resetting to 0th index(optional)
dropdownlist1.SelectedIndex = 0;
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;
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();
}
You can simply do like this.
if (ddl.Items.FindByValue("value") != null) {
ddl.SelectedValue = "value";
};
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.