Accept ENTER Key from the TextBox

Discussion in 'ASP .Net Web Controls' started by JP, Oct 20, 2003.

  1. JP

    JP Guest

    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
    JP, Oct 20, 2003
    #1
    1. Advertising

  2. 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.webcontrols
    > Path: cpmsftngxa06.phx.gbl
    > Xref: cpmsftngxa06.phx.gbl

    microsoft.public.dotnet.framework.aspnet.webcontrols:15504
    > NNTP-Posting-Host: TK2MSFTNGXA08 10.40.1.160
    > X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet.webcontrols
    >
    > 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
    >
    >
    Mike Moore [MSFT], Oct 20, 2003
    #2
    1. Advertising

  3. JP

    JP Guest

    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.webcontrols
    >> Path: cpmsftngxa06.phx.gbl
    >> Xref: cpmsftngxa06.phx.gbl

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

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

    >
    >.
    >
    JP, Oct 21, 2003
    #3
  4. 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.webcontrols
    > Path: cpmsftngxa06.phx.gbl
    > Xref: cpmsftngxa06.phx.gbl

    microsoft.public.dotnet.framework.aspnet.webcontrols:15527
    > NNTP-Posting-Host: TK2MSFTNGXA09 10.40.1.161
    > X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet.webcontrols
    >
    > 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.webcontrols
    > >> Path: cpmsftngxa06.phx.gbl
    > >> Xref: cpmsftngxa06.phx.gbl

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

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

    > >
    > >.
    > >

    >
    Mike Moore [MSFT], Oct 22, 2003
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. bsm
    Replies:
    0
    Views:
    1,281
  2. BrianDH

    Enter Key H*ll, control enter key submit button

    BrianDH, Jan 12, 2005, in forum: ASP .Net Web Controls
    Replies:
    3
    Views:
    363
    Anders Matthiessen
    Jan 12, 2005
  3. M P
    Replies:
    1
    Views:
    451
  4. Matt
    Replies:
    1
    Views:
    493
    Michael Winter
    Mar 6, 2004
  5. HopfZ
    Replies:
    0
    Views:
    534
    HopfZ
    Aug 28, 2006
Loading...

Share This Page