user70192
user70192

Reputation: 14204

ASP.NET and JQuery - Default Button Click

I have an ASP.NET page that is basically a search form. My code for this form looks like this:

<form id="mainForm" runat="server">
  Search For: <asp:TextBox ID="searchTB" runat="server" /> 
  <input id="myButton" runat="server" type="button" value="Go" 
    onclick="disableButton(this);" onserverclick="myButton_Click" />

  <script type="text/javascript">
    function disableButton(b) {
      b.disabled = true;
      b.value = "Searching...";
    }
  </script>
</form>

This code works fine as long as a user manually clicks the button. However, if a user hits the "Enter" key, the search is not performed. My question is, how do I set an HTML button to the default button in an ASP.NET page when client-side scripting is involved?

Thank you!

Upvotes: 1

Views: 1241

Answers (3)

Mike
Mike

Reputation: 276

For enter to work by default, your button needs to be the first one on the page and it should be type="submit" rather than type="button".

You could also try changing your input to a standard asp.net button. If you change your javascript function to return true, the post click of the button should still work. However to bind 2 events to the button you may need to add the client side click in your page load method.

myButton.Attributes.Add("onclick", "return disableButton(this)");

Upvotes: 0

Dave Swersky
Dave Swersky

Reputation: 34810

Have you tried the onkeypress event? Bind it to the same function and it should work.

Upvotes: 1

Related Questions