title: Prevent postback on Enter in a asp.net inputfield
body: <p>I have a problem with the key Enter in javascript and asp.net</p>
  
  <p>I have a control like this with a textchanged event which does a find but I want to control it when the user does enter</p>
  
  <pre><code>&lt;asp:TextBox ID="TextBox1" runat="server" onkeyup="EnterEvent(event)" AutoPostBack="true" OnTextChanged="TextBox1_TextChanged" /&gt;
  </code></pre>
  
  <p>That's why I created this javascript function which works very well. Because it avoids the Enter postback at any character input</p>
  
  <pre><code>    function EnterEvent(e) {
          var keycode = (e.keyCode ? e.keyCode : e.which);
          if (keycode == 13) {
              return true;
          }
          else {
              return false
          }
      }
  </code></pre>
  
  <p>And then I wanted to control when the TextBox has content, so I changed the js like this.</p>
  
  <pre><code>    function EnterEvent(e, ctrl) {
          var keycode = (e.keyCode ? e.keyCode : e.which);
          if (keycode == 13) {
              return ctrl.value.length &gt; 2;
          }
          else {
              return false
          }
      }
  </code></pre>
  
  <p>The control</p>
  
  <pre><code>&lt;asp:TextBox ID="TextBox1" runat="server" onkeyup="EnterEvent(event, this)" AutoPostBack="true" OnTextChanged="TextBox1_TextChanged" /&gt;
  </code></pre>
  
  <p>But nothing happens. Anytime that I do Enter, the page postback.</p>
  
  <p>I also added this in the code behind on the load page</p>
  
  <pre><code>            TextBox1.Attributes.Add("onkeypress", "EnterEvent(event, this);");
              TextBox1.Attributes.Add("onkeyup", "EnterEvent(event, this);");
              TextBox1.Attributes.Add("onkeydown", "EnterEvent(event, this);");
  </code></pre>
  
  <p>My page is still doing postback at Enter key. The idea is to stop the Enter postback if at least has 3 characters. Any idea or another approach?</p>
  
  <p><strong>> -----------------EDIT--------------------</strong></p>
  
  <p>I added this function before the EnterEvent</p>
  
  <pre><code>    $(function () {
          $(':text').bind('keydown', function (e) { 
              if (e.keyCode == 13) 
                  e.preventDefault();
          });
      });
  </code></pre>
  
  <p>But it blocks the Enter in all the page. Enter doesn't work AT ALL.</p>
  
  <p><strong>> -----------------EDIT 2--------------------</strong></p>
  
  <p>Well, I succeded! As I said in a previous comment, I tried like in the past adding a dummy button control and I transfered the event from the textbox and calling its click event. <strong>It's not CLEAN AT ALL but it works</strong> and I'm in a hurry. Thank you all you guys for answering. If someone can help still I would appreciate it. I'll continue reviewing this question. Thanks.</p>
  
  <pre><code>    function EnterEvent(e, ctrl) {
          var keycode = (e.keyCode ? e.keyCode : e.which);
          if (keycode == 13 &amp;&amp; ctrl.value.length &gt; 2) {
              $('[id$=Button1]').click();
          }
          else {
              return false;
          }
      }
  
  &lt;asp:TextBox ID="TextBox1" runat="server" onkeyup="EnterEvent(event, this)" /&gt;
  &lt;asp:Button ID="Button1" runat="server" OnClick="TextBox1_TextChanged" style="visibility:hidden;width:0;"/&gt;
  </code></pre>