从DropDownList中删除列表项 [英] Removing list items from the DropDownList

查看:318
本文介绍了从DropDownList中删除列表项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个gridview里面一个DropDownList,是从当在GridView加载的数据源加载。可能有人请告诉我我该怎么去循环DropDownList的以及基于if条件移除从列表中的某些项目。

I have a dropdownlist inside a gridview, that is loaded from a datasource when the gridview loads. Could someone please show me how I go about looping the dropdownlist and removing certain items from the list based on a if condition.

推荐答案

没有需要循环的项目,只要找到该项目,并删除它:

No need to loop the items, just find the item and remove it :

ListItem itemToRemove = myDropDown.Items.FindByValue("value");
if (itemToRemove != null)
{
    myDropDown.Items.Remove(itemToRemove);
}

或者,如果你知道要删除的项的索引,使用方法RemoveAt移除:

Or if you know the index of the item to remove, use RemoveAt method :

myDropDown.Items.RemoveAt(0);

但是,如果你想反正循环,这里是循环:

But if you want to loop anyway, here is the loop :

foreach (ListItem item in myDropDown.Items)
{
    // your stuff
}

这篇关于从DropDownList中删除列表项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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