Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Web Controls > Accept ENTER Key from the TextBox

Reply
Thread Tools

Accept ENTER Key from the TextBox

 
 
JP
Guest
Posts: n/a
 
      10-20-2003
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

 
Reply With Quote
 
 
 
 
Mike Moore [MSFT]
Guest
Posts: n/a
 
      10-20-2003
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.


--------------------
> Content-Class: urn:content-classes:message
> From: "JP" <>
> Sender: "JP" <>
> Subject: Accept ENTER Key from the TextBox
> Date: Mon, 20 Oct 2003 09:33:54 -0700
> Lines: 19
> Message-ID: <07e101c39727$f3fba5a0$>
> MIME-Version: 1.0
> Content-Type: text/plain;
> charset="iso-8859-1"
> Content-Transfer-Encoding: 7bit
> X-Newsreader: Microsoft CDO for Windows 2000
> X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4910.0300
> Thread-Index: AcOXJ/P7qdmv8j4QTFyAwzwhzA63PQ==
> Newsgroups: microsoft.public.dotnet.framework.aspnet.webcontro ls
> Path: cpmsftngxa06.phx.gbl
> Xref: cpmsftngxa06.phx.gbl

microsoft.public.dotnet.framework.aspnet.webcontro ls:15504
> NNTP-Posting-Host: TK2MSFTNGXA08 10.40.1.160
> X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet.webcontro ls
>
> 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
>
>


 
Reply With Quote
 
 
 
 
JP
Guest
Posts: n/a
 
      10-21-2003
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

>-----Original Message-----
>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.
>
>
>--------------------
>> Content-Class: urn:content-classes:message
>> From: "JP" <>
>> Sender: "JP" <>
>> Subject: Accept ENTER Key from the TextBox
>> Date: Mon, 20 Oct 2003 09:33:54 -0700
>> Lines: 19
>> Message-ID: <07e101c39727$f3fba5a0$>
>> MIME-Version: 1.0
>> Content-Type: text/plain;
>> charset="iso-8859-1"
>> Content-Transfer-Encoding: 7bit
>> X-Newsreader: Microsoft CDO for Windows 2000
>> X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4910.0300
>> Thread-Index: AcOXJ/P7qdmv8j4QTFyAwzwhzA63PQ==
>> Newsgroups:

microsoft.public.dotnet.framework.aspnet.webcontro ls
>> Path: cpmsftngxa06.phx.gbl
>> Xref: cpmsftngxa06.phx.gbl

>microsoft.public.dotnet.framework.aspnet.webcontr ols:15504
>> NNTP-Posting-Host: TK2MSFTNGXA08 10.40.1.160
>> X-Tomcat-NG:

microsoft.public.dotnet.framework.aspnet.webcontro ls
>>
>> 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
>>
>>

>
>.
>

 
Reply With Quote
 
Mike Moore [MSFT]
Guest
Posts: n/a
 
      10-21-2003
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.

--------------------
> Content-Class: urn:content-classes:message
> From: "JP" <>
> Sender: "JP" <>
> References: <07e101c39727$f3fba5a0$>

<>
> Subject: RE: Accept ENTER Key from the TextBox
> Date: Tue, 21 Oct 2003 06:13:53 -0700
> Lines: 163
> Message-ID: <0ce001c397d5$2ce3a310$>
> MIME-Version: 1.0
> Content-Type: text/plain;
> charset="iso-8859-1"
> Content-Transfer-Encoding: 7bit
> X-Newsreader: Microsoft CDO for Windows 2000
> Thread-Index: AcOX1SzjI0DmEzdhQmaYSeNDTmzbSA==
> X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4910.0300
> Newsgroups: microsoft.public.dotnet.framework.aspnet.webcontro ls
> Path: cpmsftngxa06.phx.gbl
> Xref: cpmsftngxa06.phx.gbl

microsoft.public.dotnet.framework.aspnet.webcontro ls:15527
> NNTP-Posting-Host: TK2MSFTNGXA09 10.40.1.161
> X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet.webcontro ls
>
> 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
>
> >-----Original Message-----
> >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.
> >
> >
> >--------------------
> >> Content-Class: urn:content-classes:message
> >> From: "JP" <>
> >> Sender: "JP" <>
> >> Subject: Accept ENTER Key from the TextBox
> >> Date: Mon, 20 Oct 2003 09:33:54 -0700
> >> Lines: 19
> >> Message-ID: <07e101c39727$f3fba5a0$>
> >> MIME-Version: 1.0
> >> Content-Type: text/plain;
> >> charset="iso-8859-1"
> >> Content-Transfer-Encoding: 7bit
> >> X-Newsreader: Microsoft CDO for Windows 2000
> >> X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4910.0300
> >> Thread-Index: AcOXJ/P7qdmv8j4QTFyAwzwhzA63PQ==
> >> Newsgroups:

> microsoft.public.dotnet.framework.aspnet.webcontro ls
> >> Path: cpmsftngxa06.phx.gbl
> >> Xref: cpmsftngxa06.phx.gbl

> >microsoft.public.dotnet.framework.aspnet.webcontr ols:15504
> >> NNTP-Posting-Host: TK2MSFTNGXA08 10.40.1.160
> >> X-Tomcat-NG:

> microsoft.public.dotnet.framework.aspnet.webcontro ls
> >>
> >> 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
> >>
> >>

> >
> >.
> >

>


 
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
capturing from text area Shft+Enter, Control+Enter, Alt+Enter and browser issue. HopfZ Javascript 0 08-28-2006 10:11 AM
using asp:textbox as search box, handling enter in textbox Imran Aziz ASP .Net 2 09-05-2005 11:00 AM
Enter Key H*ll, control enter key submit button BrianDH ASP .Net Web Controls 3 01-12-2005 08:29 PM
Replace Tab Key to Return Key (Enter Key) from Web Forms? M P ASP General 1 08-06-2004 08:32 AM
trigger HTML button by enter ENTER key Matt Javascript 1 03-06-2004 07:01 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57