Click here to Skip to main content
11,483,586 members (68,656 online)
Click here to Skip to main content

Tagged as

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

, 24 Jul 2011 CPOL 3.4K 1
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) { ...
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)

Share

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... Pin
rm9825-Jul-11 21:14
memberrm9825-Jul-11 21: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 | Terms of Use | Mobile
Web02 | 2.8.150520.1 | Last Updated 24 Jul 2011
Article Copyright 2011 by mahesh__kumar__sharma
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid