Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / General / July 2005

Tip: Looking for answers? Try searching our database.

Enter Key on Form

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
chuckdfoster - 22 Jul 2005 19:10 GMT
I have a form that with a datagrid with textboxes and a submit button.  When
the user is in a textbox and they hit enter to try to go to a different
line, instead they submit the form before they are done entering their data.
I know this is how it is supposed to work, but is there anyway to disable
the enter key from hitting the submit button?

Thanks in advance for any advice!

Signature

Chuck Foster
Programmer Analyst
Eclipsys Corporation - St. Vincent Health System

TJS - 22 Jul 2005 20:54 GMT
many have asked this before, look for a javascript solution thru google

>I have a form that with a datagrid with textboxes and a submit button.
>When the user is in a textbox and they hit enter to try to go to a
[quoted text clipped - 3 lines]
>
> Thanks in advance for any advice!
Joe Fallon - 23 Jul 2005 04:33 GMT
"How can I disable enterkey in a form. "
====================================================================

Try using some javascript.
I added it to my Base page that I inherit from and inject it to each child
page.
(As a bonus you get to set the initial focus for any control)

======================================================================
Add code like this to any child page where you want to set focus to a
control or kill the enter key:

Public Sub New()
   MyBase.New()

  'set the focus to the UserID text box:
  InitialFocus = txtUserId

   'Do Not kill the Enter key on this page
   Me.killEnterKey = False

   'Kill the Enter key on this page
   'Me.killEnterKey = True

 End Sub

======================================================================
Add this code to your javascript library which is loaded for each page:

function NetscapeEventHandler_KeyDown(e) {
 if (e.which == 13 && e.target.type != 'textarea' && e.target.type !=
'submit') { return false; }
 return true;
}

function MicrosoftEventHandler_KeyDown() {
 if (event.keyCode == 13 && event.srcElement.type != 'textarea' &&
event.srcElement.type != 'submit')
   return false;
 return true;
}

======================================================================
Add this to the top of your Base page:

Public InitialFocus As Control
Protected KillEnterKey As Boolean = True

======================================================================
Protected Overridable Sub Page_PreRender(ByVal sender As Object, ByVal e As
EventArgs)

Dim sb As New StringBuilder

   '-- Focus
   sb.Append("<script language='javascript'>")
   If Not InitialFocus Is Nothing Then
     sb.Append("document.getElementById('" & InitialFocus.ClientID &
"').focus();")
   End If

   '-- Append to the Browser Title set on an instance level

   'Code to call the Enter button kill javascript in library
   If KillEnterKey = True Then
     sb.Append("var nav = window.Event ? true : false;")
     sb.Append("if (nav) {")
     sb.Append("window.captureEvents(Event.KEYDOWN);")
     sb.Append("window.onkeydown = NetscapeEventHandler_KeyDown;")
     sb.Append("} else {")
     sb.Append("document.onkeydown = MicrosoftEventHandler_KeyDown;")
     sb.Append("}")
   End If

   sb.Append("</script>")
   RegisterStartupScript("My JScript", sb.ToString())

End Sub

======================================================================
Signature

Joe Fallon

>I have a form that with a datagrid with textboxes and a submit button.
>When the user is in a textbox and they hit enter to try to go to a
[quoted text clipped - 3 lines]
>
> Thanks in advance for any advice!

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.