Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Perform Button click event when user press Enter key in Textbox

<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

like image 398
jams Avatar asked May 10 '11 09:05

jams


People also ask

How do you trigger button click on enter?

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.

How can you avoid button click event when Enter key is pressed in a text box?

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.

How do you submit a form when Enter key is pressed?

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.


2 Answers

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...

like image 185
Harun Avatar answered Sep 25 '22 08:09

Harun


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> 
like image 26
Muhammad Akhtar Avatar answered Sep 22 '22 08:09

Muhammad Akhtar