' 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();
}
允许用户一次删除多个项
在 ItemTemplate(如果使用,还有 AlternatingItemTemplate)中,添加 CheckBox Web 服务器控件并将其 ID 属性设置为“删除”。请确保该 CheckBox 控件的 AutoPostBack 属性为 false。通过右击 DataList 控件并从菜单中选择“结束模板编辑”,关闭模板编辑。
将 Button Web 服务器控件添加到 Web 窗体页。将 Text 属性设置为“全部删除”,并将 ID 属性设置为 DeleteAll。此按钮不添加到 DataList 模板之一。
为“全部删除”按钮的 Click 事件创建方法。在方法中:
依次通过 DataList 控件的 Items 集合,按顺序提取每一项。
在项内,使用该项的 FindControl 方法获取步骤 1 的 CheckBox 控件,然后测试其 Checked 属性。
如果该框处于选中状态,则从数据源中删除相应的项。
将 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
// 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();
}
' Visual Basic
Private Sub DataList1_EditCommand(ByVal source As Object, _
ByVal e As System.Web.UI.WebControls.DataListCommandEventArgs) _
Handles DataList1.EditCommand
DataList1.EditItemIndex = e.Item.ItemIndex
DataList1.DataBind()
End Sub
// C#
private void DataList1_EditCommand(object source,
System.Web.UI.WebControls.DataListCommandEventArgs e)
{
DataList1.EditItemIndex = e.Item.ItemIndex;
DataList1.DataBind();
}
为 UpdateCommand 事件创建一个方法,该方法读取当前项中的编辑控件值,并将其写回到数据源。若要获取该项中特定控件的值,请使用 Item 事件参数对象的 Control.FindControl 方法。更新数据源后,通过将 EditItemIndex 属性设置为 -1,将该项切换出编辑模式,然后重新绑定到数据源。
安全说明 Web 窗体页中的用户输入可能包含潜在的恶意客户端脚本。默认情况下,Web 窗体页验证用户输入是否不包括脚本或 HTML 元素。有关更多信息,请参见脚本利用和在 Web 应用程序中防止脚本利用。
' Visual Basic
Private Sub DataList1_UpdateCommand(ByVal source As Object, _
ByVal e As System.Web.UI.WebControls.DataListCommandEventArgs) _
Handles DataList1.UpdateCommand
Dim quantity As TextBox
quantity = CType(e.Item.FindControl("quantity"), TextBox)
Dim newQuantity As String = quantity.Text
' Add code here to update the Quantity value of the data source
' using the newQuantity variable. Note that newQuantity is a
' string and would have to be converted to a number.
DataList1.EditItemIndex = -1
DataList1.DataBind()
End Sub
// C#
private void DataList1_UpdateCommand(object source,
System.Web.UI.WebControls.DataListCommandEventArgs e)
{
TextBox quantity;
quantity = (TextBox)(e.Item.FindControl("Quantity"));
string newQuantity = quantity.Text;
// Add code here to update the Quantity value of the data source
// using the newQuantity variable. Note taht newQuantity is a
// string and would have to be converted to a number.
DataList1.EditItemIndex = -1;
DataList1.DataBind();
}
为 CancelCommand 事件创建一个方法,该方法将 EditItemIndex 属性设置为 -1,然后重新绑定到数据源:
' Visual Basic
Private Sub DataList1_CancelCommand(ByVal source As Object, _
ByVal e As System.Web.UI.WebControls.DataListCommandEventArgs) _
Handles DataList1.CancelCommand
' Switch out of edit mode.
DataList1.EditItemIndex = -1
DataList1.DataBind()
End Sub
// C#
private void DataList1_CancelCommand(object source,
System.Web.UI.WebControls.DataListCommandEventArgs e)
{
// Switch out of edit mode.
DataList1.EditItemIndex = -1;
DataList1.DataBind();
}