Export (0) Print
Expand All

GridView::RowCommand Event

Occurs when a button is clicked in a GridView control.

Namespace:  System.Web.UI.WebControls
Assembly:  System.Web (in System.Web.dll)

public:
 event GridViewCommandEventHandler^ RowCommand {
	void add (GridViewCommandEventHandler^ value);
	void remove (GridViewCommandEventHandler^ value);
}
<asp:GridView OnRowCommand="GridViewCommandEventHandler" />

The RowCommand event is raised when a button is clicked in the GridView control. This enables you to provide an event-handling method that performs a custom routine whenever this event occurs.

Buttons within a GridView control can also invoke some of the built-in functionality of the control. To perform one of these operations, set the CommandName property of a button to one of the values in the following table.

CommandName value

Description

"Cancel"

Cancels an edit operation and returns the GridView control to read-only mode. Raises the RowCancelingEdit event.

"Delete"

Deletes the current record. Raises the RowDeleting and RowDeleted events.

"Edit"

Puts the current record in edit mode. Raises the RowEditing event.

"Page"

Performs a paging operation. Sets the CommandArgument property of the button to "First", "Last", "Next", "Prev", or a page number to specify the type of paging operation to perform. Raises the PageIndexChanging and PageIndexChanged events.

"Select"

Selects the current record. Raises the SelectedIndexChanging and SelectedIndexChanged events.

"Sort"

Sorts the GridView control. Raises the Sorting and Sorted events.

"Update"

Updates the current record in the data source. Raises the RowUpdating and RowUpdated events.

Although the RowCommand event is raised when a button listed in the previous table is clicked, it is recommended that you use the events listed in the table for the operation.

A GridViewCommandEventArgs object is passed to the event-handling method, which enables you to determine the command name and command argument of the button clicked.

NoteNote

To determine the index of the row that raised the event, use the CommandArgument property of the event argument that is passed to the event. The ButtonField class automatically populates the CommandArgument property with the appropriate index value. For other command buttons, you must manually set the CommandArgument property of the command button. For example, you can set the CommandArgument to <%# Container.DataItemIndex %> when the GridView control has no paging enabled.

For more information about handling events, see Consuming Events.

A Visual Studio Web site project with source code is available to accompany this topic: Download.

The following example demonstrates how to use the RowCommand event to add the name of a customer from a GridView control to a ListBox control when a row's Add button is clicked.

No code example is currently available or this language may not be supported.

The following example demonstrates how to use the RowCommand event to update the price of a product when a row's button is clicked. This example has the paging functionality enabled for the GridView control and sets the CommandArgument property of the Button control to the appropriate row index.

No code example is currently available or this language may not be supported.

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Show:
© 2014 Microsoft