I have been playing around with a search control and i have noticed that when you try and press enter from within the textbox it submits the form but doesnt click the search button (like i want it to).
My markup is:
<div> <span>Search</span> <asp:TextBox ID='txtSearch' runat='server' Width='170' onkeydown='if ((event.which && event.which == 13) || (event.keyCode && event.keyCode == 13)) {$('#<%=lkbSearch.ClientID %>').click();return false;} else return true; '> </asp:TextBox> </div> <asp:LinkButton ID='lkbSearch' runat='server' CssClass='searchButton'> <asp:Image ID='imgSearch' runat='server' ImageUrl='~/Images/Master/button_go.gif' /> </asp:LinkButton>
i added the onkeydown event to the textboxes, and it runs, but when i try to use JQuery to call the Click() function from the button it does nothing.
How can i make it so when enter is pressed in the textbox it ‘clicks’ the button?
After playing around with a few different things i finally came up with the solution.
I wrapped the textbox and button in an asp:panel control and set the default button to the ID of my LinkButton only to find that panels cant have LinkButtons as the DefaultButton.
But changing the link button to an ImageButton and using its ID in the DefaultButton filed of the panel works perfectly.