enter key behavior in RadGrid

11 posts, 0 answers
  1. Joe Loux
    Joe Loux avatar
    16 posts
    Member since:
    Mar 2007

    Posted 07 Aug 2008 Link to this post

    Is there any way to disable the return key behavior in a RadGrid? Thanks. 
  2. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 08 Aug 2008 Link to this post

    Hi Joe,

    You can try the following code snippet.

    ASPX:
    <ClientSettings AllowKeyboardNavigation="true"  > 
                     <ClientEvents OnKeyPress="OnKeyPress"  /> 
                </ClientSettings> 

    JS:
     <script> 
         function OnKeyPress(key) 
         { 
          var e = window.event;      
          if(e.keyCode==13)      
          {    
            return false;
             
          }       
          
         } 
         
        </script> 


    Thanks
    Shinu.

  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Steve Newbery
    Steve Newbery avatar
    103 posts
    Member since:
    Apr 2005

    Posted 10 Nov 2008 Link to this post

    Shinu, I tried this, but it doesn't always trap the Enter key.

    I want to stop the Enter key firing the Insert command, but the JS code is only invoked when the key is pressed in a form text box,and not when I hit Enter when just viewing the grid.

    Do you know how I can fix this?

    It's a pity there is no declarative way to control this.
  5. ManniAT
    ManniAT avatar
    877 posts
    Member since:
    Nov 2003

    Posted 08 Mar 2009 Link to this post

    I sit here and mess arround with the f... enter key for hours now.
    I search your forums a lot - and also found this approach - but it does not help.

    My Problem - I have a grid, where the user can change exaxtly one field (the others are read only).
    My columns are (in this order) - Editcolumn, ReadOnly, ReadOnly, RadNumbericTextBox, ReadOnly, DeleteColum.

    Now the user edits (lets say) row 3 - enters a number and presses (as used to from other software) the enter key.
    Now the changes made are lost and the first row changes to edit mode.

    Like this the grid is absolutely unusable - sorry but my customers press enter I can't avoid this.
    By the way I use inline Edit - but even with Pop this occures.

    Pleas help

    Manfred
  6. ManniAT
    ManniAT avatar
    877 posts
    Member since:
    Nov 2003

    Posted 08 Mar 2009 Link to this post

    By the way - I found a solution now - here at your forums.

    I had to attach a handler in code behind (setting OnKeyPress(this,event) in ASPX seem to pass wrong parameters.).
    Anyhow - in ItemDataBound I added
    txtbx.ClientEvents.OnKeyPress = "KeyPressed"
    Where txtbx is my RadNumericTextBox.

    On the client I added the script:

            <script type="text/javascript">  
                function KeyPressed(maskedTextBox, e) {  
                    if (e.get_domEvent().rawEvent.keyCode == 13) {  
                        e.get_domEvent().preventDefault()  
                        e.get_domEvent().stopPropagation()  
                    }  
                }  
            </script> 
     
    And now it works - enter simply does nothing.
    I have still the problem when someon klicks enter somewhere on the page the first line gets edited - but that's not very common.

    My Question - could I (extending this script) enable an "update postback" if the user hits enter.
    And (optional) a "CancelUpdate" when the user presses escape.
    ---Somewehre I heared that ESC does not fire in IE. - Anyhow this would work for firefox.
    ---For IE i can place a hidden reset input which works for the ecs key.

    Regards

    Manfred
  7. ManniAT
    ManniAT avatar
    877 posts
    Member since:
    Nov 2003

    Posted 09 Mar 2009 Link to this post

    Hi,

    I finally got it going.
    Since every control in a datarow starts with the same name prefix (decoration) I could easily do a hack using this behaviour.
    There is one (not drastic) side effect in my solution - sometimes a cursour stays visible at the position where formerly the RadNumericTextBox has been before I do the update.

    I know to little about client possibilities to solve all I want - but at least I could fix this.
    The really complete thing would look like this:
    If on Edit1 and I get enter - move focus to Edit2 - if on Edit2 move focus to....
    If EditX is the last input - update.
    Anyhow - at the current situation I have only one editable field so it is good enough to fire an update / cancel if Enter / Esc is pressed.

    Here my code:
        <script type="text/javascript">  
                function KeyPressed(ctrl, e) {  
                    if (e.get_domEvent().rawEvent.keyCode == 13) {  
                        __doPostBack(ctrl._clientID.replace('neNumValues', 'UpdateButton').replace(/_/g, "$"), '');  
                        e.get_domEvent().preventDefault();  
                        e.get_domEvent().stopPropagation();  
                    }  
                    if (e.get_domEvent().rawEvent.keyCode == 27) {  
                        __doPostBack(ctrl._clientID.replace('neNumValues', 'CancelButton').replace(/_/g, "$"), '');  
                        e.get_domEvent().preventDefault();  
                        e.get_domEvent().stopPropagation();  
                    }  
                }  
            </script> 

    It is a hack I know - but it works (except the "orphan cursor") like a charm.
    Two things are left open (not for this, but maybe good for a "complete" solution):
    Is it possible to add such a handler in ASCX also? (KeyPressed(this,event) - does not work)
    Is it possible to move the focus in such a handler? (simulating a TAB key could be a solution I guess)

    Regards

    Manfred
  8. Nikolay Rusev
    Admin
    Nikolay Rusev avatar
    2285 posts

    Posted 10 Mar 2009 Link to this post

    Hello Manfred,

    Unfortunately RadGrid does not have this behavior build in.
    For your convenience I've prepared sample application which demonstrate similar behavior.
    Please find it attached to this post.

    Best wishes,
    Nikolay
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Check out the tips for optimizing your support resource searches.
  9. ManniAT
    ManniAT avatar
    877 posts
    Member since:
    Nov 2003

    Posted 10 Mar 2009 Link to this post

    Hi Nikolay,

    thanks for your help -- but I see no attachment in your post!

    Regars

    Manfred
  10. Nikolay Rusev
    Admin
    Nikolay Rusev avatar
    2285 posts

    Posted 10 Mar 2009 Link to this post

    Hello Manfred,

    Sorry about this.
    I am re-attaching the project.

    Regards,
    Nikolay
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Check out the tips for optimizing your support resource searches.
  11. ManniAT
    ManniAT avatar
    877 posts
    Member since:
    Nov 2003

    Posted 10 Mar 2009 Link to this post

    Hi,

    thank you for this - nice thing - and great to see a lambda in samples - as I remember it ist the first time I saw it at telerik samples.

    Keep on doing this great support -- and lets hope Rume made no error in this post

    http://www.telerik.com/community/forums/aspnet-ajax/editor/toolbars-reformating.aspx#761492

    :))))

    Regards

    Manfred
  12. Daniel Aquere
    Daniel Aquere avatar
    85 posts
    Member since:
    Mar 2011

    Posted 25 Jul 2011 Link to this post

    Hi Nikolay and guys,

    Very useful this sample.

    Please, one doubt...
    The sample has only text fields, in my project I have GridDropDownColumn, GridNumericColumn, GridDateTimeColumn and GridTemplateColumn with checkbox and RadNumericTextBox. 

    Is this approach doesn´t work with this columns types?

    Thanks, Best

    Daniel
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017