How to prevent the enter key causing a form postback .net 1.1

J

Julia B

Hi all

I've got a very complex form with loads of text boxes and buttons on it.
It's causing me all sorts of problems when users press the enter key and
cause a postback.

I need to disable the enter key. I've done some surfing and found some
Microsoft recommended code but it doesn't seem to work and I'm wondering what
I've done wrong. If possible I'd like to disable the enter key for the whole
form not for each text box (that would be sooooo complicated to implement).

I'm using .Net 1.1 and vb.

Here's what I've got at the moment.

<HEAD>
<title>Raise CSD</title>
<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
<meta content="Visual Basic .NET 7.1" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content="http://schemas.microsoft.com/intellisense/ie5"
name="vs_targetSchema">
<SCRIPT language="javascript">
function DisableKeyPress()
{
//alert(window.event.keyCode)
if (event.keyCode == 13)
{
event.cancelbubble = true;
event.returnvalue = false;
}
}
</SCRIPT>
</HEAD>
<body onKeyPress="DisableKeyPress()" vLink="#000000" aLink="#d9d5d2"
link="#d7451a" MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server" >

then the code for the rest of the form.....

Thanks in advance!
Julia
 
C

Cowboy \(Gregory A. Beamer\)

In the past, I did this with JavaScript, similar to what you have here.
Only, as I remember it, it wired up the Cancel event on body load and not
for each key press. I would google JavaScript enter key or even
event.keyCode == 13 for more suggestions.
 
P

Paolo opg

Hi all

I've got a very complex form with loads of text boxes and buttons on
it. It's causing me all sorts of problems when users press the enter
key and cause a postback.

I need to disable the enter key. I've done some surfing and found some
Microsoft recommended code but it doesn't seem to work and I'm
wondering what I've done wrong. If possible I'd like to disable the
enter key for the whole form not for each text box (that would be
sooooo complicated to implement).
[cut]

then the code for the rest of the form.....

Thanks in advance!
Julia


not sure if you need to avoid unexpected form submission or you want to
disable enter key...

should be the first, did you try to feed a 'false' value to the 'onSubmit'
event of the form?


--
Paolo opg

BE AWARE that this post uses a fake reply-to address
to contact me write to:
janickg ( at ) hotmail ( dot ) com
--
 
J

Julia B

Have found this and it works :)

<Script language=javascript>
//this disables the enter key to stop form submission when the user
presses enter
document.onkeydown = doKey;

function doKey(E) {
if (event.keyCode==13)return false;
}
</script>

Thanks!
 
J

Julia B

Paulo

Thanks. I've found some javascript that does disable the entry key and sorts
my problem out. What are the reasons for not doing it this way? Wht should I
feed a fals value to the onSubmit event?

Julia

Paolo opg said:
Hi all

I've got a very complex form with loads of text boxes and buttons on
it. It's causing me all sorts of problems when users press the enter
key and cause a postback.

I need to disable the enter key. I've done some surfing and found some
Microsoft recommended code but it doesn't seem to work and I'm
wondering what I've done wrong. If possible I'd like to disable the
enter key for the whole form not for each text box (that would be
sooooo complicated to implement).
[cut]

then the code for the rest of the form.....

Thanks in advance!
Julia


not sure if you need to avoid unexpected form submission or you want to
disable enter key...

should be the first, did you try to feed a 'false' value to the 'onSubmit'
event of the form?


--
Paolo opg

BE AWARE that this post uses a fake reply-to address
to contact me write to:
janickg ( at ) hotmail ( dot ) com
 
C

Cowboy \(Gregory A. Beamer\)

Just be sure to test on other browsers, as some JavaScript only works with
some. Worst case is creating branches for different browsers.
 
J

Julia B

This should be OK for me as it's for an intranet application and all users
will using IE.

Thanks for the warning anyway.

Julia
 
P

Paolo opg

Paulo

Thanks. I've found some javascript that does disable the entry key and
sorts my problem out. What are the reasons for not doing it this way?
Wht should I feed a fals value to the onSubmit event?

Julia

regarding first question, no reason; choose the solution you prefer.

about second question, maybe you miss some knowledge about client side
events in web forms?

as an example, try to put this form in a page:
<form name="frm" action="http://www.google.com" onSubmit="return false;">
<input type="submit" value="click me">
</form>

and click the button.

you will notice that nothing happens; change false with true inside
onSubmit and click again (after a page reload, obviously).

put a js function into the onSubmit event and you have another way to solve
your issue.

as usual, a solution may fit/work or not depending on your environment
and/or your requirements.


--
Paolo opg

BE AWARE that this post uses a fake reply-to address
to contact me write to:
janickg ( at ) hotmail ( dot ) com
--
 

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

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top