<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional"> <ContentTemplate> <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Send" /> </ContentTemplate> </asp:UpdatePanel>
I have to perform Button1
click event when user press Enter key
in Textbox1
To trigger a click button on ENTER key, We can use any of the keyup(), keydown() and keypress() events of jQuery. keyup(): This event occurs when a keyboard key is released. The method either triggers the keyup event, or to run a function when a keyup event occurs.
Handle the button's OnClientClick and point that to a JS function and use "return false;" to stop the postback. Or use an HTML input button and set the onclick event of that.
To submit the form using 'Enter' button, we will use jQuery keypress() method and to check the 'Enter' button is pressed or not, we will use 'Enter' button key code value. Explanation: We use the jQuery event. which to check the keycode on the keypress.
Put your form inside an asp.net panel control and set its defaultButton attribute with your button Id. See the code below:
<asp:Panel ID="Panel1" runat="server" DefaultButton="Button1"> <asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional"> <ContentTemplate> <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Send" /> </ContentTemplate> </asp:UpdatePanel> </asp:Panel>
Hope this will help you...
In the aspx page load event, add an onkeypress
to the box.
this.TextBox1.Attributes.Add( "onkeypress", "button_click(this,'" + this.Button1.ClientID + "')");
Then add this javascript to evaluate the key press, and if it is "enter," click the right button.
<script> function button_click(objTextBox,objBtnID) { if(window.event.keyCode==13) { document.getElementById(objBtnID).focus(); document.getElementById(objBtnID).click(); } } </script>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With