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

enter key behavior in RadGrid

10 Answers 462 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Joe Loux
Top achievements
Rank 1
Joe Loux asked on 07 Aug 2008, 05:33 PM
Is there any way to disable the return key behavior in a RadGrid? Thanks. 

10 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 08 Aug 2008, 06:50 AM
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.

0
Steve Newbery
Top achievements
Rank 1
answered on 10 Nov 2008, 01:49 PM
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.
0
ManniAT
Top achievements
Rank 2
answered on 08 Mar 2009, 07:55 PM
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
0
ManniAT
Top achievements
Rank 2
answered on 08 Mar 2009, 08:51 PM
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
0
ManniAT
Top achievements
Rank 2
answered on 09 Mar 2009, 09:00 AM
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
0
Nikolay Rusev
Telerik team
answered on 10 Mar 2009, 10:30 AM
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.
0
ManniAT
Top achievements
Rank 2
answered on 10 Mar 2009, 11:34 AM
Hi Nikolay,

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

Regars

Manfred
0
Nikolay Rusev
Telerik team
answered on 10 Mar 2009, 05:23 PM
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.
0
ManniAT
Top achievements
Rank 2
answered on 10 Mar 2009, 07:06 PM
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
0
Daniel Aquere
Top achievements
Rank 2
answered on 25 Jul 2011, 08:05 PM
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
Tags
Grid
Asked by
Joe Loux
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Steve Newbery
Top achievements
Rank 1
ManniAT
Top achievements
Rank 2
Nikolay Rusev
Telerik team
Daniel Aquere
Top achievements
Rank 2
Share this question
or