Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Postback caused by pressing the "enter" key

Reply
Thread Tools

Postback caused by pressing the "enter" key

 
 
Cindy
Guest
Posts: n/a
 
      03-03-2004
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
 
Reply With Quote
 
 
 
 
Teemu Keiski
Guest
Posts: n/a
 
      03-03-2004
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) 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



 
Reply With Quote
 
 
 
 
Charlie Nilsson [MSFT]
Guest
Posts: n/a
 
      03-03-2004

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" <(E-Mail Removed)>
> References: <(E-Mail Removed) >
> 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: <#(E-Mail Removed)>
> 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" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed) 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

>
>
>


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
STRUTS- <html:submit> does not submit on pressing Enter key Megha Vishwanath Java 2 10-02-2009 12:52 PM
VoIPfone - pressing hash key T. UK VOIP 13 09-16-2005 08:46 AM
Favorite by pressing Key Thaqalain Computer Support 4 08-26-2005 09:28 PM
Pressing <ENTER> in text input control causes a postback ! peshrad ASP .Net 4 08-26-2005 02:32 PM
Get text in TextBox without pressing Enter key mg ASP .Net 5 07-14-2003 02:09 PM



Advertisments