通过


ListItemCollection.Remove 方法

定义

从集合中删除 a ListItem

重载

名称 说明
Remove(String)

ListItem 集合中删除由指定字符串表示的项。

Remove(ListItem)

从集合中移除指定的 ListItem 值。

Remove(String)

ListItem 集合中删除由指定字符串表示的项。

public:
 void Remove(System::String ^ item);
public void Remove(string item);
member this.Remove : string -> unit
Public Sub Remove (item As String)

参数

item
String

一个 String 表示要从集合中移除的项。

示例

以下示例演示 RemoveListItemCollection 的方法。 网页包含一个 ListBox 控件,其中包含其中的一些列表项和 TextBox 一个名为的 Delete控件。 用户在控件中 TextBox 输入要删除的项的文本。 控件Click的事件处理程序从ListItemCollection对象中删除所选项,因此从控件中删除ListBoxButton1

ListItem myListItem = new ListItem(Delete.Text.ToLower(),Delete.Text.ToLower());
// Check whether the 'ListItem' is present in the 'ListBox' or not.
if(ItemCollection.Contains(myListItem))
{
   String deleteString=Delete.Text;
   // Delete the listitem entered by the user in textfield.
   ItemCollection.Remove(deleteString.ToLower());
   Message.Text="<font color='green'><b>Deleted Successfully</b></font>";
}
else
{
  Message.Text="<font color='red'><b>No ListItem with the given name is present in the ListBox for deletion.</b></font>";
}               

     Dim myListItem As ListItem = new ListItem(Delete.Text.ToLower(),Delete.Text.ToLower())
     ' Check whether the 'ListItem' is present in the 'ListBox' or not.
     If(ItemCollection.Contains(myListItem)) Then
     
        Dim deleteString As String =Delete.Text
        ' Delete the listitem entered by the user in textfield.
        ItemCollection.Remove(deleteString.ToLower())
        Message.Text="<font color='green'><b>Deleted Successfully</b></font>"
     Else
     
       Message.Text="<font color='red'><b>No ListItem with the given name is present in the ListBox for deletion.</b></font>"
     End If

注解

Remove使用该方法从集合中删除ListItem对象。 此方法使用item参数文本创建对象ListItem,然后从集合中删除该ListItem对象。 指定的item文本必须与现有ListItem对象的属性和文本完全匹配Value;否则,不会删除任何项。

另请参阅

适用于

Remove(ListItem)

从集合中移除指定的 ListItem 值。

public:
 void Remove(System::Web::UI::WebControls::ListItem ^ item);
public void Remove(System.Web.UI.WebControls.ListItem item);
member this.Remove : System.Web.UI.WebControls.ListItem -> unit
Public Sub Remove (item As ListItem)

参数

item
ListItem

ListItem 从集合中移除的集合。

示例

下面的代码示例演示如何创建 ListItemCollection 对象、将项添加到集合以及从集合中删除项。 在此示例中,命名ListItemCollectionlistBoxData用作所调用ListBox1控件的ListBox数据源,调用ddBoxDataListItemCollection方用作所调用DropDownList1控件的DropDownList数据源。 若要在完全正常工作的示例中查看此代码,请参阅 WebControl 类主题。

//Set the SelectedIndex to -1 so no items are selected.
// The new item will be set as the selected item when it is added.
DropDownList1.SelectedIndex = -1;
// Add the selected item to DropDownList1.
DropDownList1.Items.Add(ListBox1.SelectedItem);
// Delete the selected item from ListBox1.
ListBox1.Items.Remove(ListBox1.SelectedItem);
' Add the selected item to DropDownList1.
DropDownList1.Items.Add(ListBox1.SelectedItem)
' Delete the selected item from ListBox1.
ListBox1.Items.Remove(ListBox1.SelectedItem)

注解

Remove使用该方法从集合中删除一个ListItem。 该方法的此实现采用 ListItem 参数指定的 item 方法,并将其从集合中删除。

另请参阅

适用于