473,322 Members | 1,431 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

disable enterkey in a form

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

Nov 18 '05 #1
1 2351
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" <Jo**@nospam.yahoo.com> wrote in message
news:ue**************@TK2MSFTNGP10.phx.gbl...
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

Nov 18 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Bruno Alexandre | last post by:
Hi guys, If I want to block a user to change a form after submiting, I add a function that will disable the submit button, but when I'm getting the form collection (using post or get...
5
by: Bob Bedford | last post by:
I create checkboxes using datas from a database (with PHP). I store all values in an array, as I must pass this value like a Form value. Now, in some cases, when a checkbox is selected, I must...
12
by: Forti2ude | last post by:
Hello, I have a simple form... <form> <select name="foo" multiple> <option value="1">one</option> <option value="2">two</option> <option value="3">three</option> </select>
2
by: cw | last post by:
Hi group, My page consist of few text boxes and a server side button control (asp:button), when i finished editing and press enter key, it will fire up the button click event. How to disable this...
2
by: nicholas | last post by:
How can I disable the enter key on an asp.net page? As the default button is the first on the page, and as the enter key clicks the default button, and as my first button on the page is th...
4
by: Chris | last post by:
I have an asp.net page say page1.aspx. The form in html code is <form id = "Form1"> And i want to disable all the fields of the form after some code steps. I had created a javascript funct: ...
0
by: Ahmad Jalil Qarshi | last post by:
Hi! I have a problem while developing some webpages.The Problem is that:- How We Can Disable The Controls Of One Web Form From Other Web Form In Asp.net? Explanation:- There Should Be Two...
1
by: Miro | last post by:
VB.net 2005 Express Say I have Dim MyString as String = "ABCDEF" and currently I have a timer that sends every 5 seconds: SendKeys.Send( MyString )
1
by: ahmadnadeem | last post by:
hi Dear, i have aproblem in richtextbox.the problem is when i press the enterkey four times then the cursor should come to the next line within the rich textbox. please tell me about the event...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.