将值添加到下拉列表 [英] Adding values to dropdownlist
本文介绍了将值添加到下拉列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好,
正在将excelsheet名称添加到dropdownlist
问题是,如果现有文件被上传以覆盖,则将dropdownlist值添加两次,我如何验证同名项目只能添加一次
Hi all,
am adding the excelsheet names to dropdownlist
problem is if the existing file is uploaded for overwrite then the dropdownlist values are added twice ,how can i validate that the item with same name must be added only once
推荐答案
作为一个简单的解决方案,编写您自己的方法以消除重复项,如下所示:
As a simple solution, write your own method to eliminate the duplicate entry as:
public static void RemoveDuplicateItems(DropDownList ddl)
{
for (int i = 0; i < ddl.Items.Count; i++)
{
ddl.SelectedIndex = i;
string currValue = ddl.SelectedItem.ToString();
for (int counter = i + 1; counter < ddl.Items.Count; counter++)
{
ddl.SelectedIndex = counter;
if (currValue == ddl.SelectedItem.ToString())
{
ddl.Items.RemoveAt(counter);
counter = counter - 1;
}
}
}
在将项目添加到下拉列表之前,请使用Contains属性检查现有项目
Before Adding Items to DropDown Lists check Existing Item using Contains Property
<pre lang="cs">DropDownList ddl= new DropDownList();
if(!ddl.Items.Contains(new ListItem("item")))
{
ddl.Items.Add(new ListItem("item"));
}
如果要调用DataBind()方法,请确保列表为先清除"
If you are calling DataBind() method make sure the List is Clear First
if(ddl.Items.Count>0)
ddl.Items.Clear();
这篇关于将值添加到下拉列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文