Well here is my way on how to handle the ENTER key on the TextBox in asp.net so it execute my method call instead the default form action.
I have a textbox and a linkbutton that invokes the search, but I also would like to hook up to that linkbutton when I press ENTER on the TextBox so I would perform the search, but by default it would invoke the form submit which is not what we want. So here is a quick solution using javascript, I know there are other one but this one is short and it works well.
<asp:TextBox ID="txtSearch" ClientIDMode="Static" runat="server" CssClass="searchbar" EnableViewState="true" ></asp:TextBox> <asp:LinkButton runat="server" ID="lnkTopSearch" onclick="btnSearch_Click" ClientIDMode="Static" CssClass="searchbutton" > </asp:LinkButton> <script type="text/javascript"> $(document).ready(function() { $('#txtSearch').keypress(function (event) { if (event.keyCode == '13') { __doPostBack('ctl00$lnkTopSearch', ''); event.preventDefault(); } }); }); </script> |