删除DropDownList中的重复值 [英] Remove duplicate values in DropDownList
本文介绍了删除DropDownList中的重复值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在我的ddlist中,我正在添加重复的项目。我希望ddlist框中的所有项目都是不同的。
我试过
Hi,
In my ddlist I am adding duplicate items. I want all the items to be distinct in the ddlist box.
I tried
if(!DropDownList1.Items.Contains(str))
{
DropDoqnList1.Items.Add(str);
}
但它显示的错误就像'ListItem'可以作为参数传递。
请帮帮忙。
But it's showing me error like only 'ListItem' can pass as an argument.
Please help.
推荐答案
您好,
搜索文本或值有两种方法在下拉菜单中。
一个是
Hello ,
there are two methods for searching the text or value in dropdown.
one is
FindByText
// search in the collection which contains the specified text
另一个是
and another one is
FindByValue
// search in the collection which contains the specified value
语法是
syntax is
drpdown.Items.FindByText(string text);
或
or
drpdown.Items.FindByValue(string value);
然后把它放进去条件
and then put it in the condition
if (drpcategory.Items.Contains(drpcategory.Items.FindByText("search text")))
{
//code..
}
谢谢
thanks
尝试:
Try:
if(!DropDownList1.Items.Contains(new ListItem(str)))
{
ListItem li = new ListItem();
li.Text = str;
li.Value = str;
DropDoqnList1.Items.Add(li);
}
只需编写此代码即可减少重复值。
It's so simple just write this code to reduce duplicate value.
if(!DropDownList1.Items.Contains(new ListItem(data)))
{
DropDownList1.Items.Add(data);
}
thanx .....: - )
thanx.....:-)
这篇关于删除DropDownList中的重复值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文