Fire button click on enter key press

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • satyabhaskar
    New Member
    • Dec 2008
    • 32

    Fire button click on enter key press

    In my web page i have one textbox and a button...and my requirement is soon i type the text in the text box and press enter key,the button code should get fired...im designing my web page in asp.net with c# code


    thanks
  • PRR
    Recognized Expert Contributor
    • Dec 2007
    • 750

    #2
    Originally posted by satyabhaskar
    In my web page i have one textbox and a button...and my requirement is soon i type the text in the text box and press enter key,the button code should get fired...im designing my web page in asp.net with c# code
    thanks
    Use following javascript
    Code:
    function fun1(e, button2){
          var evt = e ? e : window.event;
          var bt = document.getElementById(button2);
          if (bt){
              if (evt.keyCode == 13){
                    bt.click();
                    return false;
              }
          }
    }
    //And on the textbox 
    TextBox1.Attributes.Add("onkeypress", "return fun1(event,'" + Button1.ClientID + "')");
    //Button1 is the button whose onclick you will call when enter is pressed on TextBox1
    Use technical sounding title for your thread..

    Comment

    • Frinavale
      Recognized Expert Expert
      • Oct 2006
      • 9749

      #3
      You could try using the "DefaultBut ton" property of a Panel...

      Place your TextBox and your Button inside a Panel and then set the Panel's "DefaultBut ton" property to your button. Any time the user is within that panel and they hit the enter key that button will be used to submit the page.

      For example:

      [code=asp]
      <asp:panel id="content" runat="server" DefaultButton=" myDefaultButton ">
      <asp:TextBox id="myTextBox" runat="server"> </asp:TextBox>
      <asp:button id="myDefaultBu tton" runat="server" text="default button" />
      </asp:panel>[/code]

      The button with the id of "myDefaultButto n" will be used to submit the page when the user is in the "content" panel.

      I think there is a DefaultButton for the page or body as well....can't remember at this time though.

      -Frinny

      Comment

      • Swati001
        New Member
        • Sep 2014
        • 1

        #4
        It's working..Good Job....

        Comment

        • scoutdickens
          New Member
          • Oct 2016
          • 1

          #5
          Thanks. After searching through a kazillion threads, this finally worked.

          Comment

          Working...