System.Web.UI.WebControls Namespace DataGrid Class
Occurs when a cancel command button associated with an item in the DataGrid control is clicked.
[ VB ]
Public Event CancelCommand As DataGridCommandEventHandler
[ C# ]
public event DataGridCommandEventHandler CancelCommand;
[ C++ ]
public: __event DataGridCommandEventHandler* CancelCommand;
In [ JScript ], you can handle the events defined by a class, but you cannot define your own.
The CancelCommand event is raised whenever a Cancel button associated with an item in the DataGrid control is clicked. The event is typically used to trigger a handler that cancels any edits made to an item in the DataGrid control.
Controls such as cancel command buttons that are defined within the items of a parent container such as the DataGrid bubbles their event up the control hierarchy to the containing DataGrid control. This provides a convenient way to assign one handler for the same command event at the DataGrid level, as only one cancel command can be activated at any time for any item in the grid. You do not have to assign the same handler for each cancel button.
Information related to the CancelCommand event is passed via a DataGridCommandEventArgs object to the method assigned to handle the event. The following DataGridCommandEventArgs 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 DataGrid. |
The following code snippets illustrate how to specify and code a handler for the CancelCommand event, to cancel any edits made to an item in a DataGrid control.
The below snippet shows how to attach a handler for the event.
<asp:datagrid id = "myDataGrid" runat = "server"
onCancelCommand = "myGridCancelHandler" ... >
The below shows how the handler method is defined, which simply resets the EditItemIndex to -1, essentially setting edit mode off. The grid is then re-bound to the source to refresh the display back to the default grid layout.
void myGridCancelHandler ( Object src, DataGridCommandEventArgs e ) {
myGrid.EditItemIndex = -1;
PopulateList ( );
}
Show me
DataGrid Members DeleteCommand EditCommand UpdateCommand Allowing Users to Edit Items in a DataGrid Control