interactive | editorial | code | resource 
Building sites > The enter key > Discuss
 

Discuss this article

What do you think of this article? - discuss here.

1) Disable enter
[Show thread]
Quite a co-incidence, I just figured out my own way to solve this problem, then later on in the day saw this article.

The code I found disables the enter key on the page - which means that no postback is done when a user presses enter... so they have to TAB to the right control, and submit that way.

Here's the code, and the URL I got it from:
http://developer.irt.org/script/1693.htm

<script language="JavaScript"><!--
var nav = window.Event ? true : false;
if (nav) {
window.captureEvents(Event.KEYDOWN);
window.onkeydown = NetscapeEventHandler_KeyDown;
} else {
document.onkeydown = MicrosoftEventHandler_KeyDown;
}

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;
}
//--></script>

2) Submit form by hitting enter - workaround
[Show thread]
I was having this issue with a single field search form, while on the same site my login form worked would submit properly by hitting the enter key. I happened to stumble across the fact that the search form was not submitting because with one field, but if I added a second text field it works. So, I simple added a second text field and made the width=0 and it works now.

3) Submit form by hitting enter
[Show thread]
I tried adding
Page.RegisterHiddenField("__EVENTTARGET", btGo.ClientID);
in the Page_Load event. This worked fine for me, although the article suggests it won't.

Then I saw (2) above. This also worked - and is my preferred method.

4) Thank you Thank you
[Show thread]
I've been searching for days about this behavior and this saved my butt! I'm new to ASP.NET and was becoming way clueless on what to do.
Thanks to everyone

5) Another solution
[Show thread]
The solution explained in the article doesn't work with a datagrid. The problem with the default button was annoying me for a while, so I created this client side JavaScript, which solves the problem:

<script language="javascript">
<!--
function ClickButton(e, LinkToClick)
{
var i = 0;
while (i < document.forms(0).elements.length)
{
if (document.forms(0).elements(i).name.indexOf(LinkToClick) > -1)
{
document.forms(0).elements(i).click();
return (true);
}
i++;
}

i=0;

while (i < document.links.length)
{
if (document.links.item(i).id.toString().indexOf(LinkToClick) > -1)
{
document.links.item(i).click();
return (true);
}
i++;
}
return (false);
}
-->
</script>

The idea of the script is connecting a button or a linkbutton to a textbox. If your datagrid contains a linkbutton which is called lbtnSubmit, you should add this attribute to your textbox:

onkeypress="if((event.which && event.which == 13) || (event.keyCode && event.keyCode == 13)) {ClickButton(event,'lbtnSubmit');return(false);}"

You don't have to use any server side code. This code works with buttons or linkbuttons inside a datagrid, or outside. If you use multiple forms in your page, you should change the JavaScript a bit to search in all forms in the page.

6) bug in IE: you need at least TWO textboxes
[Show thread]
I tried out the TieButton solution. It didn't work at first, although the generated page-source looked fine to me. After a long search in several newsgroups, I found out (as mentioned in another message above) that you need to have at least TWO textboxes in your form.
This is a known issue of IE. If you just have the one textbox (as I did) and you hit enter in it, the form will just do a postback, but will NOT perform the button-click you were hoping for. Everything suddenly worked perfectly after I added a second textbox - you can just use somthing like:
<input type=text style="visibility:hidden">.

Anyway, thanks for the great TieButton solutoin. Now my webcontrol is completely reusable again, since I don't have to add any code whatsoever on the form that holds the control.

7) Try This two liner.
[Show thread]
Place line 1 into your Page_Load routine, then open your html page WebForm1.aspx and click the HTML button and modify the <body> text.

Use for <asp:button> and <asp:textbox>

line 1: this.TextBox1.Attributes.Add ("onkeydown", "javascript:if(event.keycode==13){btnSearch.click();return false;}");

line 2: <body MS_POSITIONING="GridLayout" onload="document.Form1.TextBox1.focus();" >

8) ImageButton Click Event through Javascript.
[Show thread]
Firstly i would like to Thank for this Solution its been of great help to us.
I have another issue with ImageButton's click event not getting triggered through Javascript in Netscape 7 but works absolutely fine in IE 6.
Is there any workaround for this?

Thanz.

9) ImageButton Click Event through Javascript.
[Show thread]
Firstly i would like to Thank for this Solution its been of great help to us.
I have another issue with ImageButton's click event not getting triggered through Javascript in Netscape 7 but works absolutely fine in IE 6.
Is there any workaround for this?

Thanz.

[Replies to this message: 10] [Post another reply]

10) XP SP2
[Show thread] [This is a reply to message 9]
Thanks for everyone's input on this - I was trawling around the web and it is good to see all the pieces of the puzzle in one place.

I thought the onkeydown option was a good one and inplemented it on my website - but then it turns out XP SP2 blocks this, grr. Same thing happends with body onload events.

So I used the Page.RegisterHiddenField("__EVENTTARGET", btGo.ClientID). but no matter how hard I tried it would not work, grr.

Anyway the second invisible input box did the trick for me, but I've set the position as well so that it does leave a nasty blank space in the displayed page

<INPUT type="text" style="VISIBILITY: hidden;POSITION: absolute">

 

Post a reply, or new message
To post a message, you must be logged in, and have
communications enabled.
Log in here, or log in and enable communications here.
In reply to:
Title:
Sub-title:
Text:
  


 
Building sites > The enter key > Discuss