Enter key causes delete event to fire

7 posts, 1 answers
  1. Nate
    Nate avatar
    26 posts
    Member since:
    Dec 2008

    Posted 22 Feb 2010 Link to this post

    Basically I have a rad grid, and the enter key is causing the delete confirm to popup.
    Is there a way to cancel the delete before the confirm pops up?

    Thanks,

    Nate
  2. Answer
    Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 22 Feb 2010 Link to this post

    Hi Nate,

    One suggestion would be showing confirm in OnCommand client event of radgrid instead of setting the ConfirmText for the column. You can chek for any condition and cancel the Delete command before showing the confirm window.

    aspx:
     
    <telerik:GridButtonColumn UniqueName="DeleteButtonColumn" text="Delete" CommandName="Delete"
    </telerik:GridButtonColumn> 

    javacript:
     
        function OnCommand(sender, args) { 
            if (args.get_commandName() == "Delete") { 
                if (condition) {   // Check for condition to cancel the event, before showing confirm 
                    args.set_cancel(true); // Cancel the event 
                } 
                if (!confirm('Do you really want to delete the record?')) { // Showing confirm 
                    args.set_cancel(true); // Cancel the event 
                } 
            } 
        } 
    Feel free to share the comments.

    Think differently :)
    Shinu.
  3. Nate
    Nate avatar
    26 posts
    Member since:
    Dec 2008

    Posted 23 Feb 2010 Link to this post

    Cool, thanks, I ended up figuring out some javascript as well, but I like your solution better, thanks!
    Nate
  4. Tim
    Tim avatar
    39 posts
    Member since:
    Oct 2010

    Posted 09 Oct 2013 Link to this post

    Hi, I'm trying to get the solution posted by Shinu to work, but I can't seem to get the keyCode for the enter key. I've tried event.keyCode but that is always 0.

    Thanks, all help is appreciated.
  5. Angel Petrov
    Admin
    Angel Petrov avatar
    1105 posts

    Posted 14 Oct 2013 Link to this post

    Hello Tim,

    The suggestion that Shinu had provided relies on the fact that a delete command is fired. Is that the case in your scenario? Could you please clarify what is the idea behind the accessing of the keyCode? This might not be needed as the delete command can be fired from both a button click or pressing the enter key. Do you want to cancel the deleting of an item only when an enter key is pressed? Please elaborate more on the exact scenario so we could provide a more precise suggestion.

    Regards,
    Angel Petrov
    Telerik
    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 the blog feed now.
  6. Fred
    Fred avatar
    1 posts
    Member since:
    Oct 2019

    Posted 16 Oct 2019 in reply to Angel Petrov Link to this post

    Dear Angel Petrov - I am a total noob helping to solve a weird little issue with the implementation of this Telerik control. It seems that the control is 'attached' (is this the right word?) to a field which gathers data - in this case, month and year. Our end user uses the control to enter a date and when pressing Enter, instead of the date being recorded, we receive the notice that the record is about to be deleted. ???

    I'm under the impression that the control has to fire a delete command? Is this corrrect? Or can the control be assigned another command - such as "not delete" or "Save date in client file xyz on server abc", etc?

    Any advice appreciated - be gentle. I'm a dullard at this stuff and am wrapping my head around it.

  7. Attila Antal
    Admin
    Attila Antal avatar
    442 posts

    Posted 21 Oct 2019 Link to this post

    Hi Fred,

    Generally this behavior appears when the keyboardboard navigation is enabled both for RadGrid and the controls in it. When the end user uses the keyboard keys to navigate and manage data, both RadGrid and the controls in it will listen to the keyboard events and the first catching the key takes the action.

    This behavior is described in the Keyboard support

    "If controls inside the grid use the same keys and combinations as the grid, and do not prevent (consume) the keydown event, you may get unexpected behavior. For example, rows may be changed in the batch edit mode while the user is attempting to navigate through a combo box list.

    To prevent this, you can set the ClientSettings.AllowKeyboardNavigation property of the grid to false. Alternatively, use widgets and code that prevents the keyboard event propagation. For example, replace RadComboBox with RadDropDownList, which prevents the up and down arrows from propagating.

    Whether a control should prevent the propagation of the keyboard events is a gray area and some controls may do that, while others do not. This is highly specific to each control and to each key."

    This behavior could also be the result of having the delete button configured to listen for the Enter key. Whenever it's pressed, click the button. 

     

    Nevertheless, I've been playing around with different scenarios and I am unable to replicate the issue.

    Here is what I have tried and it does not fire the delete command when the Enter key is pressed:

     

    Grid Markup

    <telerik:RadGrid ID="RadGrid1" runat="server" AllowPaging="True" Width="800px" OnNeedDataSource="RadGrid1_NeedDataSource">
        <ClientSettings AllowKeyboardNavigation="true">
        </ClientSettings>
        <MasterTableView AutoGenerateColumns="true" DataKeyNames="OrderID">
            <Columns>
                <telerik:GridTemplateColumn HeaderText="Template Column">
                    <ItemTemplate>
                        <%# Eval("OrderDate") %>
                    </ItemTemplate>
                    <EditItemTemplate>
                        <telerik:RadDatePicker ID="RadDatePicker1" runat="server" EnableKeyboardNavigation="true"></telerik:RadDatePicker>
                    </EditItemTemplate>
                </telerik:GridTemplateColumn>
                <telerik:GridButtonColumn CommandName="Delete"></telerik:GridButtonColumn>
            </Columns>
        </MasterTableView>
    </telerik:RadGrid>

     

    C# - Code Behind

    protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
    {
        RadGrid1.DataSource = Enumerable.Range(1, 5).Select(x => new
        {
            OrderID = x,
            OrderDate = DateTime.Now.AddDays(x),
            Freight = x * .1,
            ShipName = "Name " + x,
            ShipCountry = "Country " + x
        });
    }

     

    It would be very helpful if you could share some more details on the current scenario, I will take a look and see what can be done to prevent that from happening.

     

    See also:

     

    Kind regards,
    Attila Antal
    Progress Telerik

    Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
Back to Top