Image Button onkeypress

Discussion in 'ASP .Net Web Controls' started by crferguson@gmail.com, Jul 25, 2007.

  1. Guest

    I know this has been beaten a hundred different ways, but I've tried
    them all and I'm stuck. I have a web control on a web form. The web
    control is for logging in and all it has are two textboxes (username
    and password) and an imagebutton Submit button. The main form also
    has a handful of image buttons. I need the Submit imagebutton to fire
    when the user hits the Enter key. Right now when Enter is pressed,
    the first button on the page fires instead, which happens to be the
    Home button, and the user is taken from the Login page to the Home
    page.

    I know this behavior is simply another "feature" of ASP.NET in that
    all asp buttons are rendered as html submit buttons, so the first one
    on the page is fired on Enter. But, I also know that hundreds of
    programmers have worked around this through a few lines of
    javascript. Every method I've tried just results in java syntax
    errors, odd behavior such as "object expected" java errors, no
    behavior at all. Here are the scripts I've tried so far to no
    avail....

    ======================
    Method 1:
    <script language="javascript">
    function SubmitForm(btnName) {
    if ( event.keyCode == 13 ) {
    event.returnValue=false;
    var btn = document.forms[0][btnName];
    btn.click();
    }
    }
    </script>

    then add an onkeypress call to SubmitForm passing the webcontrol's
    ibSubmit button clientID name.

    ======================
    Method2:
    function document.onkeydown() {
    if ( event.keyCode == 13 ) {
    Form1.WebControlForm1_ibSubmit.click();
    event.returnValue=false;
    }
    }

    ======================
    Method3:
    <script>
    function submitForm()
    {
    if (event.keyCode == 13)
    {
    Form1.submit();
    }
    }
    </script>

    then add an onkeydown call in the main page's Body tag or in the
    password textbox's attributes

    ======================
    Method4:
    <script>
    function clickButton() {
    if (event.keyCode == 13) {
    document.form.button1.click();
    return false;
    }
    }
    </script>

    then add an onkeydown call in the main page's Body tag or in the
    password textbox's attributes

    ======================
    Method5:
    <script>
    function catchEnter(){
    if(event.keyCode == 13)
    __doPostBack('<%=ibSubmit.ClientID%>','');
    return false;
    else
    return true;
    }
    </script>

    then add an onkeypress call in the main page's Body tag or in the
    password textbox's attributes. Here I've tried dynamically
    determining the imagebutton's client id as above, and I've hardcoded
    the resulting client ID directly into that function.

    ======================

    Any help would be greatly appreciated. I've been looknig at this too
    long and can't figure out why nothing is working. I know I can use
    different types of buttons, but this spec is company determined and
    needs to be an imagebutton control due to reasons unrelated to this
    post.

    Thank all!!

    Cory
    , Jul 25, 2007
    #1
    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. S. Justin Gengo
    Replies:
    0
    Views:
    496
    S. Justin Gengo
    Aug 26, 2003
  2. =?Utf-8?B?ZGhucml2ZXJzaWRl?=

    JS OnKeyPress causes button click

    =?Utf-8?B?ZGhucml2ZXJzaWRl?=, Aug 24, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    713
    Ferret Face
    Aug 25, 2005
  3. =?Utf-8?B?ZGhucml2ZXJzaWRl?=

    OnKeyPress = "reload page"

    =?Utf-8?B?ZGhucml2ZXJzaWRl?=, Aug 24, 2005, in forum: ASP .Net
    Replies:
    3
    Views:
    4,888
    Eliyahu Goldin
    Aug 24, 2005
  4. =?Utf-8?B?cm9kY2hhcg==?=

    onkeypress event

    =?Utf-8?B?cm9kY2hhcg==?=, Nov 22, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    420
    =?Utf-8?B?cm9kY2hhcg==?=
    Nov 22, 2005
  5. Image Button onkeypress

    , Jul 20, 2007, in forum: ASP .Net Web Controls
    Replies:
    1
    Views:
    548
    Stump
    Aug 12, 2010
Loading...

Share This Page