Postback caused by pressing the "enter" key

Discussion in 'ASP .Net' started by Cindy, Mar 3, 2004.

  1. Cindy

    Cindy Guest

    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
    Cindy, Mar 3, 2004
    #1
    1. Advertising

  2. Cindy

    Teemu Keiski Guest

    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" <> wrote in message
    news:...
    > 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
    Teemu Keiski, Mar 3, 2004
    #2
    1. Advertising

  3. 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" <>
    > References: <>
    > 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: <#>
    > Newsgroups: microsoft.public.dotnet.framework.aspnet
    > NNTP-Posting-Host: 194.100.25.226
    > Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP12.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" <> wrote in message
    > news:...
    > > 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

    >
    >
    >
    Charlie Nilsson [MSFT], Mar 3, 2004
    #3
    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. mg
    Replies:
    5
    Views:
    1,021
    Vikram.NET
    Jul 14, 2003
  2. Megha Vishwanath
    Replies:
    2
    Views:
    9,825
    tutorialsmaster
    Oct 2, 2009
  3. Chinnala
    Replies:
    3
    Views:
    448
    Gaurav Vaish \(www.Edujini-Labs.com\)
    Jan 10, 2007
  4. Replies:
    4
    Views:
    962
  5. BrianDH

    Enter Key H*ll, control enter key submit button

    BrianDH, Jan 12, 2005, in forum: ASP .Net Web Controls
    Replies:
    3
    Views:
    389
    Anders Matthiessen
    Jan 12, 2005
Loading...

Share This Page