如何以编程方式搜索C#DropDownList [英] How to Search Through a C# DropDownList Programmatically
问题描述
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 ...
这是什么是读取或扫描每个下拉列表中的第一个值或索引,基于我在textbox1中的条目,不幸的是,它只识别第一个值或索引,我需要找出如何扫描整个下拉列表中的每个if语句的所有值,以找到匹配的textbox1值。有没有人有任何建议?
谢谢,
DFM
foreach(ListItem li in dropdownlist1.Items)
{
if(li.Value == textBox1.text)
{
//该选项的值与TextBox匹配。在这里处理东西
}
}
这是我建议如何查看价值在下拉列表中。
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 (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?
Thank you,
DFM
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.
这篇关于如何以编程方式搜索C#DropDownList的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!