Reputation: 6868
I am having one user control which contains search functionality and when i load this user control by default i am setting focus to this search textbox
:
<asp:TextBox ID="txtSearch" runat="server"></asp:TextBox>
<asp:ImageButton ID="btnSearch" runat="server" ToolTip="Search" OnClick="btnSearch_Click" />
Now when i press enter key on textbox then control goes on my master page load event and that master page load event contains one method which redirects to respective url.
But this happens only on enter and control goes to master page load events and i want to stop this.
So how to prevent this??
Upvotes: 2
Views: 2694
Reputation: 6337
I suggest you to follow below approach. Put the textbox and button inside of a Panel and set the default button to the button you want to be activated on enter.
<asp:Panel ID="panel" runat="server" DefaultButton="btnSearch">
<asp:TextBox id="txtSearch" runat="server" />
<asp:ImageButton ID="btnSearch" runat="server" ToolTip="Search" OnClick="btnSearch_Click" />
</asp:Panel>
Upvotes: 2
Reputation: 7490
Prevent enter key on textbox
$('[id*="txtSearch"]').keypress(function(e){
if ( e.which == 13 ) return false;
});
Using C#, create KeyPress event and use this code inside it.
if (e.KeyCode == Keys.Enter)
{
e.SuppressKeyPress=true;
}
You can create event using onkeypress="__doPostBack(this.name,'OnKeyPress');"
on textbox.
<asp:TextBox ID="txtSearch" runat="server" onkeypress="__doPostBack(this.name,'OnKeyPress');"></asp:TextBox>
Upvotes: 1
Reputation: 29036
You can disable the Enter-Key by using the following script:
$('TextBox').keypress(function(event) {
if (event.keyCode == 13) {
event.preventDefault();
}
});
Upvotes: 1