Reputation: 929
I want to stop the IsPostBack fired from the enter key pressed at a TextBox. The textBox can not be multiline.
I'm trying this:
<asp:TextBox ID="kemetTextBox" runat="server" Width="215px">
</asp:TextBox>
<script type="text/javascript">
$(document).ready(function () {
$("#kemetTextBox").keyup(function (e) {
if (e.keyCode == 13) {
Search();
return false;
}
});
});
</script>
But it stills reloading the page.
Data: Visual Studio 2010, Asp.net, C# as codebehind.
Thanks
Upvotes: 0
Views: 1669
Reputation: 13266
Instead of KeyUp, use keyDown
<script type="text/javascript">
$(document).ready(function () {
$("#kemetTextBox").keydown(function (e) {
if (e.keyCode == 13) {
Search();
e.preventDefault();
return false;
}
});
});
</script>
Upvotes: 1
Reputation: 102418
Just set the AutoPostBack="False"
like this:
<asp:TextBox ID="kemetTextBox" runat="server" Width="215px" AutoPostBack="False">
Use the AutoPostBack property to specify whether an automatic postback to the server will occur when the TextBox control loses focus. Pressing the ENTER or the TAB key while in the TextBox control is the most common way to change focus.
Adding to this you can do this too:
<asp:TextBox ID="kemetTextBox" runat="server" Width="215px" onkeydown="return (event.keyCode!=13);">
Source: Disable Enter key in TextBox to avoid postback in ASP.Net
Upvotes: 3