title: How to set the default button for a TextBox in ASP.Net?
body: <p>I have a page that has two buttons, btnSearch and btnAddUser. The aspx page is as below. As you can see the default button is btnAddUser. But I would like to set the default button to btnSearch when I type something in the text box txtFilter. I added a JavaScript function clickButton, and in the page_load I added following code. The problem is when I press enter after I type something in the text box, both btnSearch and  btnAddUser are clicked. Is there any way I could do so that btnAddUser is not clicked.</p>
  
  <pre><code> txtFilter.Attributes.Add("onkeypress", "return clickButton(event, '" + btnSearch.ClientID + "')");
  
  &lt;asp:Panel ID="MainPanel" runat="server" DefaultButton="btnAddUser"&gt;
      &lt;table align="center" width="900"&gt;
          &lt;tr&gt;
              &lt;td align="left" width="70%"&gt;
                  &lt;asp:TextBox ID="txtFilter" runat="server" Width="200"&gt;&lt;/asp:TextBox&gt;
                  &lt;asp:Button ID="btnSearch" runat="server" Text="Search" 
                      onclick="btnSearch_Click" CausesValidation="false" /&gt;
              &lt;/td&gt;
              &lt;td align="right" width="30%"&gt;
                  &lt;asp:Button ID="btnAddUser" runat="server" Text="Add User" Width="85px" 
                      CausesValidation="False" onclick="btnAddUser_Click" /&gt;
              &lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
  ...
  &lt;/asp:Panel&gt;
  
  function clickButton(e, buttonid) {
              var evt = e ? e : window.event;
              var bt = document.getElementById(buttonid);
              if (bt) {
                  if (evt.keyCode == 13) {
                      bt.click();
                      return false;
                  }
              }
          }
  </code></pre>
  
  <p>UPDATE:
  After googling, i found a solution as below. It seems working. But it is not working on Firefox? Anyone know how to resolve it?</p>
  
  <pre><code>function clickButton(e, buttonid) {
              var evt = e ? e : window.event;
              var keycode = evt.keyCode || evt.which || evt.charCode;
              var bt = document.getElementById(buttonid);
              if (bt) {
                  if (keycode == 13) {
                      evt.cancelBubble = true;
                      evt.returnValue = false;
                      if (evt.stopPropagation) {
                          evt.stopPropagation();
                          evt.preventDefault();
                      }
                      bt.click();
                      return false;
                  }
              }
          }
  </code></pre>