获取下拉列表中的第一个现有项目C# [英] Get the first existing item in dropdown list C#
问题描述
你熟悉SQL中的LIKE条件吗?是否可以对绑定下拉列表执行相同的方法?就像我搜索CH一样,程序会在下拉列表中找到以CH开头的第一个现有项目并选择它。
感谢您的帮助!
我尝试了什么:
我不知道该怎么做如果这是不可能的,我将只选择索引0.
Hi,
Are you familiar with LIKE condition in SQL? Is it possible to do the same approach to a bound dropdown list? like if I search for 'CH', the program will find the 1st existing item in dropdown list that starts with 'CH' and selects it.
Thanks for you help!
What I have tried:
I have no idea about what to do here. If this is not even possible, i will just select index 0.
推荐答案
也许你可以使用客户端脚本/插件来过滤有界下拉列表?
以下是一些例子:
http://jsearchdropdown.sourceforge.net/
https://www.sitepoint.com/13-jquery-selectboxdrop-down-plugins/
Maybe you can use the client side script/plugin to filter the bounded dropdownlist?
Here are some example:
http://jsearchdropdown.sourceforge.net/
https://www.sitepoint.com/13-jquery-selectboxdrop-down-plugins/
希望以下为您服务
Hope following works for you
foreach (ListItem item in this.cmb.Items)
{
if (item.Text.StartsWith("CH", StringComparison.OrdinalIgnoreCase))
{
item.Selected = true;
break;
}
else
item.Selected = false;
}
}
将组合框与数据库中的数据绑定后,这是当然的
This is ofcourse after you bind your combo box with the data from the database
假设列表已经是数据绑定,您只需在下拉列表中设置SelectedValue属性。
Assuming the list is already data bound you can simply set the SelectedValue property on your dropdown list.
list.DataSource = GetListItems(); //< - 从某个地方获取数据。
list.DataValueField =ValueProperty;
list.DataTextField =TextProperty;
list.DataBind();
list.SelectedValue = myValue.ToString();
list.DataSource = GetListItems(); // <-- Get your data from somewhere.
list.DataValueField = "ValueProperty";
list.DataTextField = "TextProperty";
list.DataBind();
list.SelectedValue = myValue.ToString();
这篇关于获取下拉列表中的第一个现有项目C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!