ItemCommand Event
Fired when any button is clicked in the RadGrid control. All bubbled events from grid items fire RadGrid.ItemCommand. Those like - Edit, Delete, or Update command events can be used to handle custom data-editing in RadGrid. Expand/Collapse items in the hierarchy or GridGroupHeaderItem -s (if grid displays grouped items), also fired after binding of detail tables, etc.
Event Parameters
-
(object)
sender- The control that fires the event
-
(GridCommandEventArgs)
e-
Event arguments that
-
(boolean)
e.CanceledIf set to True the event will be canceled
-
(object)
e.CommandArgumentArguments that are set to the Control's CommandName property. (Usually Button but it could be other Control in the Template)
-
(string)
e.CommandNameName defined in the CommandName property of the Control.
-
(object)
e.CommandSourceThe Control that fired the Command for the Grid.
-
(GridItem)
e.ItemGets the Item that initiated the command. Can be any of the Grid items that Inherit the
GridItem
class (e.g. GridDataItem, GridCommandItem, GridHeaderItem, GridFilteringItem, GridEditFormItem, etc...)
-
-
Attaching the event
In the Markup
<telerik:RadGrid ID="RadGrid1" runat="server" OnItemCommand="RadGrid1_ItemCommand">
</telerik:RadGrid>
In the Code behind
protected void Page_Init(object sender, EventArgs e)
{
RadGrid1.ItemCommand += RadGrid1_ItemCommand;
}
The event handler
protected void RadGrid1_ItemCommand(object sender, GridCommandEventArgs e)
{
object commandArguments = e.CommandArgument;
bool canceled = e.Canceled;
string commandName = e.CommandName;
object source = e.CommandSource;
GridItem item = e.Item;
}
Examples
Capturing commands
Grid known Commands
protected void RadGrid1_ItemCommand(object sender, GridCommandEventArgs e)
{
if (e.CommandName == RadGrid.InitInsertCommandName)
{
// Clicking on the "Add New Record" button to make the grid enter into insert mode
}
else if (e.CommandName == RadGrid.PerformInsertCommandName)
{
// When clicking on the Insert button after the values have been defined
}
else if (e.CommandName == RadGrid.EditCommandName)
{
// When clicking on the Edit button to put the Grid into edit mode
}
else if (e.CommandName == RadGrid.UpdateCommandName)
{
// When clicking on the Update button after the changes are done
}
else if (e.CommandName == RadGrid.DeleteCommandName)
{
// When the delete button is clicked for an item
}
else if (e.CommandName == RadGrid.PageCommandName)
{
// When Paging back and forth
}
else if (e.CommandName == RadGrid.SortCommandName)
{
// When sorting
}
else if (e.CommandName == RadGrid.FilterCommandName)
{
// When filtering
}
}
Custom Commands
The name defined in the CommandName property of the Button can be captured as follows
<telerik:RadGrid ID="RadGrid1" runat="server" OnItemCommand="RadGrid1_ItemCommand">
<MasterTableView>
<Columns>
<telerik:GridTemplateColumn>
<ItemTemplate>
<telerik:RadButton runat="server" ID="RadButton1" Text="Custom Command 1" CommandName="MyCustomCommand1" AutoPostBack="true" />
<telerik:RadButton runat="server" ID="RadButton2" Text="Custom Command 2" CommandName="MyCustomCommand2" AutoPostBack="true" />
</ItemTemplate>
</telerik:GridTemplateColumn>
</Columns>
</MasterTableView>
</telerik:RadGrid>
protected void RadGrid1_ItemCommand(object sender, GridCommandEventArgs e)
{
if (e.CommandName == "MyCustomCommand1")
{
// execute some logic
}
else if (e.CommandName == "MyCustomCommand2")
{
// execute some logic
}
}