从列表框中删除所选项目 [英] Delete Selected Item from Listbox

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

问题描述

SPWeb web = SPContext.Current.Web;
    SPList list = web.Lists["MyList"];
    if (list != null)
    {
        for (int i = list.ItemCount - 1; i >= 0; i--)
        {
            list.Items[i].Delete();
        }
        list.Update();
    }





我在分享列表中有更多项目然后当我在列表框中只选择一个项目并删除下面的代码,然后它删除我列表中的项目,而不仅仅是我选择的,有人可以帮助我吗?



I have more Items in an sharepoint list then when i in an listbox select only one item and delete if with the following code , then it deletes al the items in my list , not just the only i have selected , can somebody help me ?

推荐答案

因为你删除了所有的项目在你的代码中!

你应该在删除它之前尝试匹配你的标准。



Because you delete all the items in your code!
You should try to match your criteria before delete it.

SPWeb web = SPContext.Current.Web;
    SPList list = web.Lists["MyList"];
    if (list != null)
    {
        for (int i = list.ItemCount - 1; i >= 0; i--)
        {
            if( list.Items[i].equals("your criteia ")){
               list.Items[i].Delete();
            }
        }
        list.Update();
    }


As you have a loop and in a loop you have put a code, which delete itmes so all items will deleted, if you want to delete only the selected element then don't take a loop and use this code,

 if (list != null)
{
     list.Items.Remove(list.SelectedItem);
        list.Update();
}


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

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