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!
Have you tried the onkeypress
event? Bind it to the same function and it should work.
ASP.NET 2.0 - Enter Key - Default Submit Button
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)");
精彩评论