Accept ENTER Key from the TextBox

J

JP

Hi,

I have a login screen and have usual stuff on it
including "Forgot Password" and "Change Password" as image
buttons along with "Login" as image button.

Now, when I enter Id/Password and hit ENTER, it's accepted
by the "Forgot Password" image button and not by "Login".
I threw my "Login" codes into "txtPassword_TextChanged"
event and it worked, but still sometimes it goes
to "Forgot Password".

Any help to make "Login" accept the ENTER Key
from "Password" text box will be much appreciated.

Thanks a million!
Regards,
JP
 
M

Mike Moore [MSFT]

Hi JP,

This is normal behavior and there is a workaround. When there are text
boxes and buttons on a form, pressing enter while in a text box will
activate the first of the buttons in the sequence they are defined in the
HTML.

One solution is to place your preferred button first in the HTML and use
grid layout to display it where ever you want.

Another solution is to capture the enter key in the text box and redirect
its behavior. Here's a sample for doing this.

First, we need to add the keydown event to each text box. You can do this
in the code behind as follows:
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
TextBox1.Attributes.Add("onkeydown", "return testForEnter()")
TextBox2.Attributes.Add("onkeydown", "return testForEnter()")
End Sub

Next, we need client-side javascript to process the key down. Here is a
sample which causes the third image button to click rather than the first.

<script>
function testForEnter() {
if (event.keyCode == 13)
{
document.all("ImageButton3").click();
event.returnValue = false;
}
}
</script>

Setting the returnValue to false prevents the first button from also firing.

Does this answer your question?

Thank you, Mike
Microsoft, ASP.NET Support Professional

Microsoft highly recommends to all of our customers that they visit the
http://www.microsoft.com/protect site and perform the three straightforward
steps listed to improve your computer’s security.

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


--------------------
 
J

JP

Hi Mike,

Thank You! I think this will get me what I am trying to
do. However, for the first attempt, it didn't work. I am
sorry, but I am still a new fish in the ocean of ASP.NET
and it would help if you could give more details on how to
use the code you gave.

This is what I did....
In the page load event I added the KeyDown event, but
changed the syntax to proper cases. Following is the exact
line I entered.
txtPassword.Attributes.Add("OnKeyDown", "return
TestForEnter()")

Now is a little confusing part for me, to add the script
in HTML codes. I added this <script></script> right after
<head></head>, not sure if that's okay!

<script>
function testForEnter() {
if (event.keyCode == 13)
{
document.all("imgLogin").click();
event.returnValue = false;
}
}
</script>

Now I run the app and for the password first character,
which is an upper case letter, I press SHIFT key and the
page's bottom left corner shows the yellow triangle
indicating an error. And the error is ...

Line: 80
Char: 1
Error: Object Expected
Code: 0
URL: http://localhost/NAVAlertData/Login.aspx

Line 80 on my HTML page is a </TR> ending tag and line 80
on my Login.aspx.vb (code behind) is commented out. So now
I don't know what to look for...

Please help....

Thanks a million again in advance!
JP
 
M

Mike Moore [MSFT]

Hi JP,

It's your capitalization. You changed attributes.add to TestForEnter.
However, you left the function as testForEnter. The initial "t" is
capitalized in one but not the other. As a side note, we usually put event
driven script inside the head section, though it will work just about
anywhere.

Thank you, Mike
Microsoft, ASP.NET Support Professional

Microsoft highly recommends to all of our customers that they visit the
http://www.microsoft.com/protect site and perform the three straightforward
steps listed to improve your computer’s security.

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

--------------------
 

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

No members online now.

Forum statistics

Threads
474,079
Messages
2,570,574
Members
47,206
Latest member
Zenden

Latest Threads

Top