Click here to Skip to main content
Click here to Skip to main content
Alternative Tip

How to make a button the default button on enter on an ASP.NET form

By , 24 Jul 2011
Rate this:
Please Sign up or sign in to vote.
function SetDefalutButton(e, buttonid)
{
   var evt = e ? e : window.event;
   if (evt.keyCode == 13)/*Search if user hit enter key*/
   {
      var bt = $('[id$='+buttonid+']');
          if (bt.length > 0)
          {
              if (evt.keyCode == 13)
              {
              eval( bt[0].href);//if it is link button
              //bt.click();//if it button
              return false;
              }
          }
   }
}

Here is how we use it:

<asp:TextBox ID="txtBox" runat="server" 
  onkeypress="return SetDefalutButton(event,'btnTest');" MaxLength="50"></asp:TextBox>

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

mahesh__kumar__sharma
Software Developer IONNOR
India India
No Biography provided

Comments and Discussions

 
GeneralReason for my vote of 1 OK for one or two field on form, but... Pinmemberrm9825-Jul-11 20:14 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web04 | 2.8.140421.2 | Last Updated 24 Jul 2011
Article Copyright 2011 by mahesh__kumar__sharma
Everything else Copyright © CodeProject, 1999-2014
Terms of Use
Layout: fixed | fluid