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

Deleting while editing puts different record into edit mode

4 Answers 83 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Raymond Andrews
Top achievements
Rank 1
Raymond Andrews asked on 20 Sep 2010, 01:25 AM
While trying to find a way to disable deleting or inserting while editing a row in a radgrid ( I could still use some help ) I stumbled across a strange side effect.  If you're editing a row, and delete one of the rows above the one you're editing, you will suddenly be editing a different item.

Using the example at http://demos.telerik.com/aspnet-ajax/controls/examples/integration/raduploadinajaxifiedgrid/defaultcs.aspx?product=grid

Click the edit button for Bellflower to put that record into edit mode
Now click the delete button for Senecio.
You will see that you are now editing Thistle.

Is this behavior by design ?  I think it could easily create user confusion, especially if they were editing/deleting from a long list of similar items.  They could accidentally delete an item, and then click the update button thinking they were posting the changes they had made, but instead would be making no changes to the newly edited item..

4 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 20 Sep 2010, 07:00 AM
Hello Raymond,

Try the following code snippet in ItemCommad event to cancel the delete/insert operation while editing.

C#:
protected void RadGrid2_ItemCommand(object source, GridCommandEventArgs e)
    {
        if ((e.CommandName == "Delete" || e.CommandName == RadGrid.InitInsertCommandName) && RadGrid2.EditItems.Count > 0)
        {
            e.Canceled = true;
        }
     }

Thanks,
Princy.

0
Raymond Andrews
Top achievements
Rank 1
answered on 20 Sep 2010, 04:09 PM
Thank you, that's elegantly simple and will help a lot.

Two small problems though
- for stopping an insert or edit it works great, but I'll need to add some kind of "hey finish your current edit first" pop-up
- My deletes are with a grid button column, with confirmation. The button (rightly) confirms before issuing it's CommandName, I can change the button to perform your check, and handle the confirmation manually

It would be nice if Telerik had commands for PreInsert, PreEdit, PreDelete, etc.. so changes to confirmations, or solutions such as yours could be implemented in a logical place

Thanks again.
0
Accepted
Princy
Top achievements
Rank 2
answered on 21 Sep 2010, 08:03 AM
Hello Raymond,

In order to achieve this, try the code shown below for cancelling delete/insert operations when grid is in editmode. Attach 'OnCommand' client event to RadGrid and show the alert/confirm dialogs accordingly.

ASPX:
<ClientSettings>
    <ClientEvents OnCommand="OnCommand" />
</ClientSettings>

Java Script:
Java Script:
<script type="text/javascript">
     function OnCommand(sender, args)
      {
        var grid = sender;
        if (args.get_commandName() == "InitInsert" && grid._editIndexes[0] >= 0)
         {
                alert("hey finish your current edit first");
                args.set_cancel(true);
         }
        if (args.get_commandName() == "Delete")
        {
                if (grid._editIndexes[0] >= 0)
                {
                  alert("hey finish your current edit first");
                  args.set_cancel(true);
                }
              else
                  var value = confirm("Are you sure you want to delete?");
                  if (!value)
                     args.set_cancel(true);
         }
      }
</script>

Thanks,
Princy.
0
Raymond Andrews
Top achievements
Rank 1
answered on 21 Sep 2010, 01:14 PM
Wow, I had planned on doing it myself, but you just saved me some work, I was thinking of doing it server-side, but javascript is probably a much better way to go .  Thanks !
Tags
Grid
Asked by
Raymond Andrews
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Raymond Andrews
Top achievements
Rank 1
Share this question
or