473,327 Members | 1,892 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,327 software developers and data experts.

Postback caused by pressing the "enter" key

Hi all you smarties out there,

I'm having a little conundrum with my asp.net page

Scenario:
I have a form (asp.net) with no code behind (as yet).
I have placed a javascript function on a server side textbox control
to do something - eg change the words on label - when the key pressed
is the "enter" key.
I can see the label change to what i expect ----- BUT .... then the
form appears to refresh itself and the label reverts back to its
original text. (and i'm suspecting that it is performing a postback)

Question:
1. Is pressing enter and causing a postback something anyone else has
experienced ?? And if so does anyone know if it is BY DESIGN ??

2. How can I prevent the form performing a postback when the enter key
is pressed (short from not using a webform) ??

thanking you in advance =)

Cindy
Nov 18 '05 #1
2 7119
Hi,

AFAIK that's one of the problems with IE, but yes causing a postback by
pressing Enter or clicking a Button are different things by default. I guess
it is by design in IE, but not necessarily with ASP.NET. :-)

One way to handle it is to check for pressed button in JavaScript and then
"forward" the action to some button if it was Enter (keycode 13). Anyway,
here's one control made for that purpose:
http://www.metabuilders.com/Tools/DefaultButtons.aspx

--
Teemu Keiski
MCP, Microsoft MVP (ASP.NET), AspInsiders member
ASP.NET Forum Moderator, AspAlliance Columnist

"Cindy" <hk*********@hotmail.com> wrote in message
news:b0**************************@posting.google.c om...
Hi all you smarties out there,

I'm having a little conundrum with my asp.net page

Scenario:
I have a form (asp.net) with no code behind (as yet).
I have placed a javascript function on a server side textbox control
to do something - eg change the words on label - when the key pressed
is the "enter" key.
I can see the label change to what i expect ----- BUT .... then the
form appears to refresh itself and the label reverts back to its
original text. (and i'm suspecting that it is performing a postback)

Question:
1. Is pressing enter and causing a postback something anyone else has
experienced ?? And if so does anyone know if it is BY DESIGN ??

2. How can I prevent the form performing a postback when the enter key
is pressed (short from not using a webform) ??

thanking you in advance =)

Cindy

Nov 18 '05 #2

Yes, this is IE's fault.

When you hit enter, your form is submitted (obviously) and IE makes a guess
about what to do. Generally, if there is only one textbox on the page, IE
will not "click" any buttons for you. If there are > 1 fields on the form,
IE will magically click the first button on the form. The server will see
separate events in either case.

Your case seems to be that ASP cannot detected the IPostBackEventHandler
(nothing matches since IE said 'no buttons were clicked'). ASP will run
the Validators, but do nothing else.

The workaround may be to:
- add a second, hidden textfield on the page, which forces IE to choose to
submit the first button on the form when someone hits <enter>.
- use the change event in the textbox to do the processing instead of
allowing IE to 'pick' an event. (though this won't work if no data is
entered and they hit <enter>.
- use script to trap the keypress and 'click' the button through scripting.
Something like....
<script>
function clickButton() {
if (event.keyCode == 13) {
document.form.button1.click();
return false;
}
}
</script>
<form runat=server id=form >
<input type=text id="textbox1" runat=server
onkeypress="return(clickButton());">
<input type=submit id="button1" runat=server
OnServerClick=button1_click >
</form>
Basically, this is a common issue, and your solution will probably be
unique to the design of your page.
--
CharlieN
VSU

This posting is provided "AS IS" with no warranties, and confers no rights.

Note: For the benefit of the community-at-large, all responses to this
message are best directed to the newsgroup/thread from which they
originated.
--------------------
From: "Teemu Keiski" <jo****@aspalliance.com>
References: <b0**************************@posting.google.com >
Subject: Re: Postback caused by pressing the "enter" key
Date: Wed, 3 Mar 2004 09:24:21 +0200
Lines: 43
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
Message-ID: <#h**************@TK2MSFTNGP12.phx.gbl>
Newsgroups: microsoft.public.dotnet.framework.aspnet
NNTP-Posting-Host: 194.100.25.226
Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTN GP12.phx.gbl
Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.framework.aspnet:215407
X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet

Hi,

AFAIK that's one of the problems with IE, but yes causing a postback by
pressing Enter or clicking a Button are different things by default. I guess it is by design in IE, but not necessarily with ASP.NET. :-)

One way to handle it is to check for pressed button in JavaScript and then
"forward" the action to some button if it was Enter (keycode 13). Anyway,
here's one control made for that purpose:
http://www.metabuilders.com/Tools/DefaultButtons.aspx

--
Teemu Keiski
MCP, Microsoft MVP (ASP.NET), AspInsiders member
ASP.NET Forum Moderator, AspAlliance Columnist

"Cindy" <hk*********@hotmail.com> wrote in message
news:b0**************************@posting.google.c om...
Hi all you smarties out there,

I'm having a little conundrum with my asp.net page

Scenario:
I have a form (asp.net) with no code behind (as yet).
I have placed a javascript function on a server side textbox control
to do something - eg change the words on label - when the key pressed
is the "enter" key.
I can see the label change to what i expect ----- BUT .... then the
form appears to refresh itself and the label reverts back to its
original text. (and i'm suspecting that it is performing a postback)

Question:
1. Is pressing enter and causing a postback something anyone else has
experienced ?? And if so does anyone know if it is BY DESIGN ??

2. How can I prevent the form performing a postback when the enter key
is pressed (short from not using a webform) ??

thanking you in advance =)

Cindy



Nov 18 '05 #3

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

Similar topics

8
by: EBG | last post by:
I have a simple email form on my site but getting screwy results because I know alot of users are hitting "enter" instead of tab when doing the form. When they hit enter the entire form is...
4
by: Simon Wigzell | last post by:
Is there a way to prevent a form submitting when you press enter on a text field? Some people press enter when they finish typing in a text field out of habit I guess unconcsciously thinking it...
1
by: Byron McClain | last post by:
.... is being consumed. I added an event handler for the "keypress" event and my delegate never gets executed. Why? I am trying to catch the "enter" key pressed event to prevent the DataGrid...
1
by: laredotornado | last post by:
Hi, I have a very simple form and I would like on Firefox and IE for the form to submit upon the user pressing enter in the textfield. Right now, I have <script type="text/javascript">...
0
by: joe | last post by:
How to disable imagebutton postback when i enter "enter" i just want postback when click mouse
0
by: roxanaislam | last post by:
Hi: I have a search form in my application which has 4 dropdown list controls, one text box, one "GO" button, one "reset" button. When the "GO" button is clicked the form gets submitted and the...
4
by: roxanaislam | last post by:
Submitting Form by pressing the "ENTER" key -------------------------------------------------------------------------------- Hi: I have a search form in my application which has 4 dropdown...
3
by: Zytan | last post by:
I can handle DoubleClick on a ListBox to respond, not when the selection changes, but when you double click one to invoke an action. This is the same as using the keys to move the selection, then...
3
by: Simon Verona | last post by:
Sorry for the repost, but this group seems to be more "active" than the group that I posted my question, and it's probably as valid here as there! I have a usercontrol, which contains a textbox...
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
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...
1
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: 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
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.