Click here to Skip to main content
15,884,099 members
Articles / Web Development / ASP.NET
Alternative
Tip/Trick

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

Rate me:
Please Sign up or sign in to vote.
3.75/5 (4 votes)
24 Jul 2011CPOL 8.7K   1   1
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) { ...
JavaScript
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.NET
<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)


Written By
Software Developer IONNOR
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

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

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

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