System.Web.UI.WebControls Namespace
.NET Framework version 2.0
Provides data for the command events of a GridView.
The GridViewCommandEventArgs provides event data for the following command events of a GridView.
The generic RowCommand event is raised whenever any button associated with a row in the GridView is clicked. This provides for programmatically determining which specific command button is clicked and take appropriate action. This event is commonly used to handle custom command buttons for the GridView control.
NOTE: The GridView control also raises other specialized events when certain buttons ( such as a cancel, delete, edit, or update button ) associated with a row in the GridView is clicked. When using these buttons, you should consider handling one of the specialized events provided by the control ( RowCancelingEdit, RowDeleting, RowEditing, or RowUpdating ).
For more information about handling events, see Web Forms Events Model.
The following example demonstrates using GridViewCommandEventArgs properties to obtain information about the currently selected item in a GridView control.
void updateCart ( Object src, GridViewCommandEventArgs e ) {
DataRow dr = Cart.NewRow ( );
// get the row index stored in the CommandArgument property
int index = Convert.ToInt32 ( e.CommandArgument );
// get the GridViewRow where the command is raised
GridViewRow selectedRow = ( ( GridView ) e.CommandSource ).Rows [ index ];
// for bound fields, values are stored in the Text property of Cells [ fieldIndex ]
string item = selectedRow.Cells [ 1 ].Text;
string price = selectedRow.Cells [ 2 ].Text;
if ( e.CommandName == "AddToCart" ) {
dr [ 0 ] = item; dr [ 1 ] = price;
Cart.Rows.Add ( dr );
}
else { // remove from Cart
CartView.RowFilter = "Item='" + item + "'";
if ( CartView.Count > 0 ) CartView.Delete ( 0 );
CartView.RowFilter = "";
}
shopCart.DataBind ( );
}
GridView GridViewCommandEventHandler RowCommand