This is a migrated thread and some comments may be shown as answers.

Cancel Edit - Row Child Control Events Still Fire?

8 Answers 214 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Andrew
Top achievements
Rank 1
Andrew asked on 08 Mar 2011, 09:42 AM
This behaviour is probably by design - but I'm confused as to why I still get the child control value changed events when I hit the Cancel link - I'd have thought they would have been discarded?

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

Sort by
0
Andrew
Top achievements
Rank 1
answered on 08 Mar 2011, 01:59 PM
...to ask the question in a different way - in an event raised by a control in an Editable row of the grid (like a TextChanged of a textbox or SelectedIndexChanged of a combo), how can I tell from the GridDataItem, if the Cancel was clicked, not the Update ?
0
Tsvetoslav
Telerik team
answered on 11 Mar 2011, 07:43 AM
Hello Andrew,

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. 

Greetings,
Tsvetoslav
the Telerik team
Registration for Q1 2011 What’s New Webinar Week is now open. Mark your calendar for the week starting March 21st and book your seat for a walk through all the exciting stuff we ship with the new release!
0
Andrew
Top achievements
Rank 1
answered on 11 Mar 2011, 09:06 AM
Unfortunately, that approach doesn't work for our needs. We have a very large data entry sheet where there are several columns of text boxes available for data entry, even when all rows are NOT in Edit mode, and therfore no Command will be invoked. Other columns have data points that are read only until you manually edit the row, then you can change them.

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.



    
0
Tsvetoslav
Telerik team
answered on 14 Mar 2011, 09:40 AM
Hi Andrew,

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.

Regards,
Tsvetoslav
the Telerik team
Registration for Q1 2011 What’s New Webinar Week is now open. Mark your calendar for the week starting March 21st and book your seat for a walk through all the exciting stuff we ship with the new release!
0
Augusto
Top achievements
Rank 1
answered on 01 Dec 2011, 09:24 AM
Hello.

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

0
Tsvetoslav
Telerik team
answered on 05 Dec 2011, 04:56 PM
Hi 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
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now
0
Augusto
Top achievements
Rank 1
answered on 06 Dec 2011, 09:00 AM
Hi Tsvetoslav.

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
0
Tsvetoslav
Telerik team
answered on 08 Dec 2011, 05:39 PM
Hi 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
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now
Tags
Grid
Asked by
Andrew
Top achievements
Rank 1
Answers by
Andrew
Top achievements
Rank 1
Tsvetoslav
Telerik team
Augusto
Top achievements
Rank 1
Share this question
or