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 to write code that finds a specific value in each dropdownlist; but, before this happens, I need to add an "if" statement saying, "if dropdownlist doesn't contain the specific value, go to next if statement, and so on". The following is an example of what I have so far:
我很难搞清楚如何编写一系列“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...
What this does is reads or scans the first value or index in each dropdownlist, based off of my entry in textbox1. Unfortunately, it only identifies the first value or index. I need to figure out how to scan through the entire dropdownlist for all values per each "if" statement to find the matching textbox1 value. Does anyone have any suggestions?
这样做是根据我在textbox1中的条目读取或扫描每个下拉列表中的第一个值或索引。不幸的是,它只识别第一个值或索引。我需要弄清楚如何在整个下拉列表中扫描每个“if”语句的所有值,以找到匹配的textbox1值。有没有人有什么建议?
Thank you,
DFM
8 个解决方案
#1
foreach (ListItem li in dropdownlist1.Items)
{
if (li.Value == textBox1.text)
{
// The value of the option matches the TextBox. Process stuff here.
}
}
That is my suggestion for how to see if the value is in the dropdownlist.
这是我建议如何查看值是否在下拉列表中。
#2
The DropDownList inherits the Items collection from the ListControl. Since Items is an Array, you can use this syntax:
DropDownList从ListControl继承Items集合。由于Items是一个数组,您可以使用以下语法:
dropdownlist1.Items.Contains(textbox1.Text) as a boolean.
dropdownlist1.Items.Contains(textbox1.Text)作为布尔值。
#3
I would make a list of Drop-down boxes and then use linq to select on it.
我会制作一个下拉框列表,然后使用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;
#4
The solutions presented work if you want to search for an exact value in a loaded combobox.
如果要在加载的组合框中搜索精确值,则解决方案可用。
This solution, searches for partial values also. It uses a search button and the text portion of the dropdown box as the search criteria
此解决方案也会搜索部分值。它使用搜索按钮和下拉框的文本部分作为搜索条件
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
#5
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();
}
#6
You can simply do like this.
你可以这样做。
if (ddl.Items.FindByValue("value") != null) {
ddl.SelectedValue = "value";
};
#7
One line of code- split for readablilty.
一行代码 - 拆分可读性。
this.DropDownList1.SelectedItem = this.DropDownList1.Items
.SingleOrDefault(ddli => ddli.value == this.textbox1.value);
#8
If you don't want to use LINQ:
如果您不想使用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;
}
}
}
#1
foreach (ListItem li in dropdownlist1.Items)
{
if (li.Value == textBox1.text)
{
// The value of the option matches the TextBox. Process stuff here.
}
}
That is my suggestion for how to see if the value is in the dropdownlist.
这是我建议如何查看值是否在下拉列表中。
#2
The DropDownList inherits the Items collection from the ListControl. Since Items is an Array, you can use this syntax:
DropDownList从ListControl继承Items集合。由于Items是一个数组,您可以使用以下语法:
dropdownlist1.Items.Contains(textbox1.Text) as a boolean.
dropdownlist1.Items.Contains(textbox1.Text)作为布尔值。
#3
I would make a list of Drop-down boxes and then use linq to select on it.
我会制作一个下拉框列表,然后使用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;
#4
The solutions presented work if you want to search for an exact value in a loaded combobox.
如果要在加载的组合框中搜索精确值,则解决方案可用。
This solution, searches for partial values also. It uses a search button and the text portion of the dropdown box as the search criteria
此解决方案也会搜索部分值。它使用搜索按钮和下拉框的文本部分作为搜索条件
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
#5
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();
}
#6
You can simply do like this.
你可以这样做。
if (ddl.Items.FindByValue("value") != null) {
ddl.SelectedValue = "value";
};
#7
One line of code- split for readablilty.
一行代码 - 拆分可读性。
this.DropDownList1.SelectedItem = this.DropDownList1.Items
.SingleOrDefault(ddli => ddli.value == this.textbox1.value);
#8
If you don't want to use LINQ:
如果您不想使用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;
}
}
}