现在的位置: 首页 > 综合 > 正文

允许用户删除 DataList Web 服务器控件中的项

2013年09月11日 ⁄ 综合 ⁄ 共 2556字 ⁄ 字号 评论关闭
   MSDN Home >  Visual Studio .NET >  Visual Basic 和 Visual C# >  创建应用程序 >  创建 Web 应用程序和服务 >  ASP.NET 服务器控件 >  可在 Web 窗体页上使用的控件 >  DataList Web 服务器控件
Visual Basic 和 Visual C# 概念  

允许用户删除 DataList Web 服务器控件中的项

可允许用户以各种方法删除 DataList 控件中的项。一种方法是在项中包含“删除”按钮,当用户单击它时立即删除该项。

另一种方法是在单个项中包括复选框。用户随后可以选中要移除的所有项,然后单击单独的“删除”按钮成批删除它们。此方法在如 MSN Hotmail 这样的程序中使用。

允许用户删除单个项

  1. ItemTemplate 中(和 AlternatingItemTemplate 中,如果要使用它的话),添加一个 ButtonLinkButton Web 服务器控件。将此按钮的 CommandName 属性设置为 delete(区分大小写)。
  2. DeleteCommand 事件创建事件处理程序。有关详细信息,请参见在 Web 窗体页中创建事件处理程序。在此方法中:
    1. 从数据源中删除项,然后重新绑定 DataList 控件。用户所单击项的索引可以通过 Item 对象的 ItemIndex 属性获得。若要获取单个控件的值,请使用 Item 事件参数对象的 FindControl 方法。
    2. 将控件重新绑定到它的数据源。

      如果数据源是 DataTable 对象,则事件处理程序可能类似如下所示:

      ' Visual Basic
      Private Sub DataList1_DeleteCommand(ByVal source As Object, _
      ByVal e As System.Web.UI.WebControls.DataListCommandEventArgs) _
      Handles DataList1.DeleteCommand
         ' Code to delete the item from the data source.
         Dim aTable As DataTable
         aTable = CType(DataList1.DataSource, DataTable)
         aTable.Rows(e.Item.ItemIndex).Delete()
         ' Bind the data after item is deleted.
         DataList1.DataBind()
      End Sub
      
      // C#
      private void DataList1_DeleteCommand(object source, 
      System.Web.UI.WebControls.DataListCommandEventArgs e)
      {
         // Code to delete the item from the data source.
         DataTable aTable = (DataTable)DataList1.DataSource;
         aTable.Rows[e.Item.ItemIndex].Delete();
         // Bind the data after the item is deleted.
         DataList1.DataBind();
      }

允许用户一次删除多个项

  1. ItemTemplate(如果使用,还有 AlternatingItemTemplate)中,添加 CheckBox Web 服务器控件并将其 ID 属性设置为“删除”。请确保该 CheckBox 控件的 AutoPostBack 属性为 false。通过右击 DataList 控件并从菜单中选择“结束模板编辑”,关闭模板编辑。
  2. Button Web 服务器控件添加到 Web 窗体页。将 Text 属性设置为“全部删除”,并将 ID 属性设置为 DeleteAll。此按钮不添加到 DataList 模板之一。
  3. 为“全部删除”按钮的 Click 事件创建方法。在方法中:
    1. 依次通过 DataList 控件的 Items 集合,按顺序提取每一项。
    2. 在项内,使用该项的 FindControl 方法获取步骤 1 的 CheckBox 控件,然后测试其 Checked 属性。
    3. 如果该框处于选中状态,则从数据源中删除相应的项。
    4. DataList 控件重新绑定到它的数据源。

      下面的示例显示 DeleteAll 按钮的事件处理程序,它使用上面所述的过程成批删除项。

      ' Visual Basic
      Private Sub DeleteAll_Click(ByVal sender As Object, _
      ByVal e As System.EventArgs) Handles DeleteAll.Click
         Dim isDeleted As Boolean
         Dim anItem As DataListItem
      
         ' Check each box and see if the item should be deleted.
         For Each anItem In DataList1.Items
            isDeleted = CType(anItem.FindControl("Delete"), CheckBox).Checked
            If isDeleted Then
               ' Add code here to delete the item, using anItem.ItemIndex.
            End If
         Next
         DataList1.DataBind()
      End Sub
      
      // C#
      protected void DeleteAll_Click(object sender, System.EventArgs e)
      {
         bool isDeleted;
      
         // Check each box and see if the item should be deleted.
         foreach (DataListItem anItem in DataList1.Items)
         {
            isDeleted = 
               ((CheckBox)anItem.FindControl("Delete")).Checked;
            if (isDeleted)
            { 
               // Add code here to delete the item, using anItem.ItemIndex.
            }
         }
         DataList1.DataBind();
      }

请参见

介绍 DataList Web 服务器控件 | 允许用户编辑 DataList Web 服务器控件中的项 | 允许用户选择 DataList Web 服务器控件中的项 | 在 Web 窗体页中创建事件处理程序 | 响应 DataList、Repeater 或 DataGrid 项中的按钮事件

抱歉!评论已关闭.