disable enterkey in a form

J

John Smith

Hi,
How can I disable enterkey in a form. I have using couple of asp textboxes,
one asp button for search and two Image Buttons, one for Back[will come
first] and other for continue[will come next]. But If I click on the text
box and perss enter key it will always execute the back Image button and the
page is redirecting to the previous page. I gave last tab order index for
this button , it didn't work. I just want execute the search button if they
click enter. How could i do this. Could any one can put some light on it.

Thanks in Advance

John
 
J

Joe Fallon

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

======================================================================
--
Joe Fallon



John Smith said:
Hi,
How can I disable enterkey in a form. I have using couple of asp
textboxes,
one asp button for search and two Image Buttons, one for Back[will come
first] and other for continue[will come next]. But If I click on the text
box and perss enter key it will always execute the back Image button and
the
page is redirecting to the previous page. I gave last tab order index for
this button , it didn't work. I just want execute the search button if
they
click enter. How could i do this. Could any one can put some light on it.

Thanks in Advance

John
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top