从列表框中删除所选项目
问题描述:
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();
}