# OnCommand

Telerik.Web.UI.GridCommandEventArgs OnCommand Property

> note To get or set property values for client API properties, you must call property accessor methods that are named with the get\_ and set\_ prefixes. For example, to get or set a value for a property such as [cancel](https://msdn.microsoft.com/en-us/library/bb310859.aspx), you call the get_cancel or set_cancel.

This event will be raised for each grid command which is about to be triggered (sorting, paging, filtering, editing, etc.) before postback/ajax request except hierarchy/grouping expand/collapse action. The event can be intercepted in order to perform a certain operation in the grid via web service or to detect what type of command is raised on the client.

| **Fired by**        | RadGrid                                                                                                                                                                                  |
| ------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| **Arguments**       | **commandName** - the name of the command which is about to be processed **commandArgument** - the command argument(s) value(s) **tableView** - returns reference to the owner TableView |
| **Can be canceled** | Yes, set eventArgs.set_cancel(true) to cancel                                                                                                                                            |

The client-side `GridCommandEventArgs` does not expose the clicked button or a `CommandSource` property. For `Edit` and `Delete` commands, the `commandArgument` is the item index. You can use it with the `tableView` to retrieve the corresponding data item when client-side binding is used:

```JavaScript
 function onCommand(sender, args) {
    var commandName = args.get_commandName();

    if (commandName == "Edit" || commandName == "Delete") {
        var itemIndex = args.get_commandArgument();
        var dataItem = args.get_tableView().get_dataItems()[itemIndex].get_dataItem();
    }
}
```

If you need the clicked button element itself, handle the button's click separately and resolve its parent row from the DOM. The server-side `CommandSource` and `Item` properties do not have client-side equivalents in `OnCommand`.

Example:

```ASP.NET
<telerik:RadGrid RenderMode="Lightweight" ID="RadGrid1" runat="server">
    <ClientSettings>
        <ClientEvents OnCommand="RaiseCommand" />
    </ClientSettings>
</telerik:RadGrid>
```

```JavaScript
function RaiseCommand(sender, eventArgs)
{
    //retrieve the current commandName and commandArgument
    var result = String.format("CommandName: {0}, CommandArgument: {1}", eventArgs.get_commandName(), eventArgs.get_commandArgument());

    if (<some_custom_condition_not_met>)
    {
        eventArgs.set_cancel(true); //cancel the command
    }
}
```

> caption How to prevent loss of user input in batch editing mode

```JavaScript
function OnCommand(sender, args) {
	var tableView = args.get_tableView();
	var batchEditingManager = sender.get_batchEditingManager();
	var isDirty = batchEditingManager.hasChanges(tableView);
	var commandName = args.get_commandName();
	//allow the user to cancel changes always
	//if there are changes allow the user only to save changes 
	var shouldAllowOperation = commandName == "RebindGrid" || !(isDirty && commandName != "BatchEdit");
	args.set_cancel(!shouldAllowOperation);
	
}
```
