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. Advertisements

  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, Mar 3, 2004
    #2
    1. Advertisements

  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.
    --------------------
     
    Charlie Nilsson [MSFT], Mar 3, 2004
    #3
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.