1

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!

1
  • The problem is related to the disabled=true. How do I disable the button, but still my the server side event fire? Commented Nov 5, 2009 at 12:17

3 Answers 3

1

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

Sign up to request clarification or add additional context in comments.

Comments

0

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)");

Comments

0

ASP.NET 2.0 - Enter Key - Default Submit Button

1 Comment

While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.