System.Web.UI.WebControls Namespace DataList Class
Occurs when a delete command button associated with an item in the DataList control is clicked.
[ VB ]
Public Event DeleteCommand As DataListCommandEventHandler
[ C# ]
public event DataListCommandEventHandler DeleteCommand;
[ C++ ]
public: __event DataListCommandEventHandler* DeleteCommand;
In [ JScript ], you can handle the events defined by a class, but you cannot define your own.
The DeleteCommand event is raised whenever a Delete button associated with an item in the DataList control is clicked. The event is typically used to trigger a handler that removes the item in the DataList control.
Controls such as delete command buttons that are defined within the items of a parent container such as the DataList bubbles their event up the control hierarchy to the containing DataList control. This provides a convenient way to assign one handler for the same command event at the DataList level, as only one delete command can be activated at any time for any item in the grid. You do not have to assign the same handler for each delete button.
Information related to the DeleteCommand event is passed via a DataListCommandEventArgs object to the method assigned to handle the event. The following DataListCommandEventArgs properties provide information specific to this event.
Property |
Description |
CommandArgument ( inherited from CommandEventArgs ) |
Gets the argument for the command. |
CommandName ( inherited from CommandEventArgs ) |
Gets the name of the command. |
CommandSource |
Gets the source of the command. |
Item |
Gets the selected item in the DataList control. |
WARNING: Deleting rows in a database table CANNOT BE UNDONE, which can result in serious data loss. In a real-world scenario, only the database administrator or an authorized user must be granted DELETE permission. The examples provided here are only for learning purposes.
The following example demonstrates how to code a handler for the DeleteCommand event to delete a single item from a DataList control.
void deleteItem ( Object src, DataListCommandEventArgs e ) {
// get key to find in data source
string empToDelete = ( ( LinkButton ) e.Item.FindControl ( "itemToDelete" ) ).Text;
// load data from Session
DataTable myTable = ( DataTable ) Session [ "myTable" ];
DataRowCollection myRows = myTable.Rows;
// get row to delete
DataRow thisRow = myRows.Find ( empToDelete );
if ( thisRow != null ) myRows.Remove ( thisRow );
// refresh data in Session
Session [ "myTable" ] = myTable;
// rebind list to updated data in Session
myList.DataSource = Session [ "myTable" ];
myList.DataBind ( );
}
Show me
The following example demonstrates how to enable multiple item deletions in a DataList control.
void deleteItems ( Object src, EventArgs e ) {
// load data from Session
DataTable myTable = ( DataTable ) Session [ "myTable" ];
DataRowCollection myRows = myTable.Rows;
// initialize counter
int deleted = 0;
// loop thru each datalist item and see if item should be deleted.
foreach ( DataListItem item in myList.Items ) {
HtmlInputCheckBox checker = ( HtmlInputCheckBox ) item.FindControl ( "checker" );
if ( checker.Checked ) {
DataRow thisRow = myRows.Find ( checker.Text );
if ( thisRow != null ) myRows.Remove ( thisRow );
deleted ++;
}
}
// refresh data in Session
Session [ "myTable" ] = myTable;
// rebind grid to updated data in Session
myList.DataSource = Session [ "myTable" ];
myList.DataBind ( );
myList.Visible = ( myGrid.Items.Count > 0 );
}
Show me
DataList Members CancelCommand EditCommand UpdateCommand Allowing Users to Delete Items in a DataList Control