如何以编程方式搜索C#DropDownList [英] How to Search Through a C# DropDownList Programmatically

查看:160
本文介绍了如何以编程方式搜索C#DropDownList的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很难想出如何编写一系列if语句,通过不同的下拉列表搜索在文本框中输入的特定值。我能够编写在每个下拉列表中找到特定值的代码;但是,在这种情况发生之前,我需要添加一个if语句,说如果dropdownlist不包含特定值,请转到下一个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 ...

这是什么是读取或扫描每个下拉列表中的第一个值或索引,基于我在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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆