So for example - I have a RadGrid with an EditCommandColumn, and a template column with a label in the ItemTemplate, and a TextBox in the EditItemTemplate.
This TextBox has a _TextChanged event which is wired up to change the column in the dataset the RadGrid is bound to.
When I hit Edit, the row goes into Edit mode, and the textbox appears. I then change the text in that textbox, but hit Cancel, rather than Update. The _TextChanged event fires first, before the RadGrid_CancelCommand - so I can't event set a bIsCancelling flag, and have an "If Not bIsCancelling" around the code in my _TextChanged event, to prevent the dataset updating code from executing.
I'm new to these controls, so I'm probably missing something obvious - I just need to know how Cancels are handled correctly with the RadGrid.
Thanks,
Andrew
8 Answers, 1 is accepted

You need to implement your update logic in the ItemCommand event - there you can get all the information needed (new values as opposed to old ones, command name, etc.). Doing the update operations in the edit form's control events is not the best approach.
Please, go through our help topics and online examples for further information, e.g. :
http://www.telerik.com/help/aspnet-ajax/grid-insert-update-delete-at-database-level.html
Hope it helps.
Tsvetoslav
the Telerik team

Instead we have implemented a novel solution where we have created our own Cancel linkbutton, this has a command argument of the DataKey of the row that is being edited. Then, in each control event handler, we can use the psuedocode :
If NOT (PostbackButton.Command = "Cancel" and PostbackButton.CommandArgument = ThisRow.DataKey) Then
'Do the Data changes
End If
Basically, this allows you to have a single row in Edit mode, but to change the data in other, non-edit rows too. If you hit Cancel in the edit row, this invokes a postback, and any changes to controls in the row that is cancelled are ignored. Changes to rows other than the Edited row are accepted. If the Update button is pressed, then ALL changes are processed.
Still I think the approach I have proposed can be implemented in your case too. Do note that any button within the grid that submits the form will invoke the grid's ItemCommand event and any button to which you have set the CommandName property to a custom command name will send that custom command name into the ItemCommand event. So you can set the CommadName property of your custom cancel link button to a custom command name and intercept it in the ItemCommand event.
As for the other controls that can invoke update operations without the row being in edit mode - then you will not have a cancel button and hence not have the problem with event sequence - so in this case you can safely go with your own approach with the update on the controls' events.
Tsvetoslav
the Telerik team

When I insert a new record in grid the ItemCreated event is fired and I do some initialization in the edit form (based on a template):
void
RadGridMain_ItemCreated(
object
sender, GridItemEventArgs e) {
if
(e.Item.GetType() ==
typeof
(GridEditFormInsertItem)) {
// Set field attributes when we are in Creation mode
GridEditFormInsertItem editItem = (GridEditFormInsertItem)e.Item;
RadComboBox cbo = (RadComboBox)editItem.FindControl(
"myCombobox"
);
...
}
}
void
RadGridMain_ItemCommand(
object
sender, GridCommandEventArgs e) {
...
}
I would like to avoid initializing the edit form controls when the user clicks on the Cancel button of the edit form.
Unfortunately the ItemCommand is fired before the ItemCreated event. I wanted to check the ((Telerik.Web.UI.RadButton)(e.CommandSource)).CommandName value in the ItemCommand and set a value in a asp:HiddenField to tell the ItemCreated handler that the source of the event was a Cancel command. But of course I can't.
Is it possible to know in the ItemCreated event if the Cancel command was triggered?
Thanks for you help
Regards
Augusto
You should have a second condition in the if clause of the ItemCreate event: if (.... && e.Item.IsInEditMode)...
Hope it helps.
Greetings, Tsvetoslav
the Telerik team

The e.Item.IsInEditMode is true even when the event is fired from the Cancel button. So I can rely on this flag to know if I'm in Cancel mode or not.
Regards
Augusto
You certainly can rely on this flag. In addition, I'd recommend that you move the initialization code from the ItemCreated event to the ItemDataBound one - the first is fired even when the grid's data is loaded from ViewState, the second one only on a true bind/rebind and as far as I understand your scenario, the latter is exactly the one you need.
Greetings,
Tsvetoslav
the Telerik team