Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

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

0.00/5 (No votes)
24 Jul 2011 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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here