System.Web.UI.WebControls Namespace
.NET Framework version 2.0
Represents the method that will handle the command events of a GridView.
[ VB ]
Public Delegate Sub GridViewCommandEventHandler ( _
ByVal src As Object, _
ByVal e As GridViewCommandEventArgs _
)
[ C# ]
public delegate void GridViewCommandEventHandler (
object src,
GridViewCommandEventArgs e
);
[ C++ ]
public __gc __delegate void GridViewCommandEventHandler (
Object* src,
GridViewCommandEventArgs* e
);
In [ JScript ], you can use the delegates in the .NET Framework, but you cannot define your own.
The declaration of your event handler must have the same parameters as the declaration for the GridViewCommandEventHandler delegate.
- src
- The source of the event.
- e
- A GridViewCommandEventArgs object that contains the event data.
When you create a GridViewCommandEventHandler delegate, you identify the method that will handle the event. To associate the event with your event handler, add an instance of the delegate to the event. The event handler is called whenever the event occurs, unless you remove the delegate.
The following example demonstrates how to code a handler for the RowCommand event to add or remove an item from a shopping cart.
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 ( );
}
GridViewCommandEventArgs GridView RowCancelingEdit RowDeleting RowEditing RowCommand RowUpdating