Simulate button click on Return key press

Discussion in 'ASP .Net' started by Dan, Feb 6, 2004.

  1. Dan

    Dan Guest

    Consider a simple form with a textbox and button.

    Is there a way to handle the common situation where a user presses "return"
    rather than clicks on the button.

    I can already detect the "return" key press using the textbox's onkeypress
    event and now what I'm looking for is something like:

    onkeypress="if 'return' key pressed, then button.click=True"

    Any ideas?
     
    Dan, Feb 6, 2004
    #1
    1. Advertising

  2. Dan

    Dan Guest

    Maybe I should try searching for Enter key first....



    You can intercept the client side enter keypress event of the text box and
    then click do what you want using javascript code.
    Here's a good example:
    http://www.kamp-hansen.dk/pages/showdoc.asp?id=28&menuid=21&menuid=18

    Or you might try using this free control.
    http://www.metabuilders.com/tools/DefaultButtons.aspx

    And here's a good article on the subject:
    http://www.allasp.net/enterkey.aspx

    --
    I hope this helps,
    Steve C. Orr, MCSD, MVP
    http://Steve.Orr.net
    Hire top-notch developers at http://www.able-consulting.com



    "Abhram Jose" <> wrote in message
    news:...
    > I have written an ASP.Net application.
    > It has several edit box to enter data. How to program in such way that

    when
    > the user hits enter key focus will go to next edit box (required control )
    > and so on ?
    > Thanks for your suggestions.
    > Abhram Jose
    >
    >


    "Dan" <> wrote in message
    news:%...
    > Consider a simple form with a textbox and button.
    >
    > Is there a way to handle the common situation where a user presses

    "return"
    > rather than clicks on the button.
    >
    > I can already detect the "return" key press using the textbox's onkeypress
    > event and now what I'm looking for is something like:
    >
    > onkeypress="if 'return' key pressed, then button.click=True"
    >
    > Any ideas?
    >
    >
    >
    >
    >
     
    Dan, Feb 6, 2004
    #2
    1. Advertising

  3. onkeypress="if(event.keyCode==13) document.getElementById('someButton').click()

    or in other words just call the buttons click() method.

    but I have no idea how to get the buttons id as the .NET framework changes all ids do something like _ctl1$_ctl4$someButton... naturally you can viewsource your page and see what id it generates and use that but that kinda hardcodes and if you change your control tree layout it might stop working.
    im sure there's a way to get around this though...
     
    =?Utf-8?B?QWRyaWphbiBKb3NpYw==?=, Feb 6, 2004
    #3
  4. Dan

    Dan Guest

    Thanks for this.

    "Adrijan Josic" <> wrote in message
    news:...
    > onkeypress="if(event.keyCode==13)

    document.getElementById('someButton').click()"
    >
    > or in other words just call the buttons click() method..
    >
    > but I have no idea how to get the buttons id as the .NET framework changes

    all ids do something like _ctl1$_ctl4$someButton... naturally you can
    viewsource your page and see what id it generates and use that but that
    kinda hardcodes and if you change your control tree layout it might stop
    working..
    > im sure there's a way to get around this though...
     
    Dan, Feb 6, 2004
    #4
  5. Dan

    xanoxate

    Joined:
    Oct 19, 2006
    Messages:
    1
    With asp.net 2.0 you can enlcose the text box and button inside a panel and then set the DefaultButton property of the panel to the id of the button, when the user presses the enter key the button will be pressed
     
    xanoxate, Oct 19, 2006
    #5
  6. Dan

    Moim

    Joined:
    Jun 6, 2007
    Messages:
    2
    Location:
    Dhaka
    Here is a quick resolution of your problem


    You can get the '_ctl1$_ctl4$someButton' button's id as follows:

    var buttonClientID = '<%=btnServerButton.UniqueID%>';
    var htmlButton = document.getElementById (buttonClientID)

    Cheers..
     
    Moim, Jun 6, 2007
    #6
  7. Dan

    maheshsase

    Joined:
    Jun 28, 2007
    Messages:
    1
    //this code is for Wndows application VS2003 C#
    //where txtSearchTerm is a textbox on which you want that Click
    //event of btnSearch to get occur.

    private void txtSearchTerm_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
    {
    if (e.KeyChar.Equals( Convert.ToChar(13) ))
    {
    this.btnSearch_Click(this.btnSearch,null);
    }

    }
     
    maheshsase, Jun 28, 2007
    #7
  8. Dan

    cathal_mchale

    Joined:
    May 18, 2009
    Messages:
    1
    Panel.DefaultButton


    Ok, this is a very old post, but just wanted to point out that the post by xanoxate is the best one for me. Thanks, I never knew this, and always thought that farting about with Javascript and ClientIDs seemed overkill and something that ASP.NET should have provided out-of-the-box, given how common the scenario is!
     
    cathal_mchale, May 18, 2009
    #8
    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. Jie
    Replies:
    7
    Views:
    6,192
    Xavier Pacheco
    Oct 9, 2004
  2. Joel

    How to simulate a key press?

    Joel, May 12, 2006, in forum: C Programming
    Replies:
    4
    Views:
    625
    doina
    May 12, 2006
  3. Max2006
    Replies:
    2
    Views:
    1,596
    Walter Wang [MSFT]
    Sep 19, 2007
  4. Replies:
    0
    Views:
    469
  5. GOKU24
    Replies:
    0
    Views:
    156
    GOKU24
    Aug 26, 2003
Loading...

Share This Page