如何动态检查下拉列表框中的冗余? [英] How to check the redundancy in the drop down list box dynamically?

查看:54
本文介绍了如何动态检查下拉列表框中的冗余?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我想动态地将当前年份添加到下拉列表中。但是,如果该值已存在于下拉列表中,则不应再次添加该值。我可以在下拉列表中添加年份。但每次加载页面时都会反复添加。如何避免这种情况?



例如:当前年份是2012年。现在下拉列表应该只包含2012年。而2013年将是下拉列表应包含2012年以及2013年等等。



提前谢谢...

Hi All,

I want to add current year to drop down list dynamically. But if the value is already present in the drop down list then it should not be added again. I am able to add year to drop down list. But it gets added again and again every time the page is loaded. How to avoid this?

For example : Current year is 2012. Now the drop down list should only contain 2012. And when 2013 will come the drop down list should contain 2012 as well as 2013 and so on.

Thanks in advance...

推荐答案

如果我理解正确,你想从DropDown中删除重复的项目。所以这是你的解决方案:(来源



If I understood correctly, you want to remove duplicate items from DropDown.. So here is your solution : (source)

public static void RemoveDuplicateItems(DropDownList ddl)
{

for (int i = 0; i < ddl.Items.Count; i++)
{
ddl.SelectedIndex = i;
string str = ddl.SelectedItem.ToString();
for (int counter = i + 1; counter < ddl.Items.Count; counter++)
{
ddl.SelectedIndex = counter;
string compareStr = ddl.SelectedItem.ToString();
if (str == compareStr)
{
ddl.Items.RemoveAt(counter);
counter = counter - 1;
}
}
}
}


这篇关于如何动态检查下拉列表框中的冗余?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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