Click here to Skip to main content
15,886,017 members
Please Sign up or sign in to vote.
1.11/5 (2 votes)
See more:
How to Fire Button Click Event when Enter key is pressed after writing some text in textbox.

ASP.NET
<form id="form1"  runat="server">
    <div>
    
    Enter text-<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <asp:Button ID="Button1" runat="server" Text="Send" Width="65px" />
    
    </div>
    </form>
Posted
Updated 22-Aug-18 5:20am
v2
Comments
Sm.Abdullah 27-Feb-13 8:49am    
why do not you put your click event code in side a function and call that function after detecting the enter key against a textbox ?

Try this



function enterEvent(e) {
if (e.keyCode == 13) {
$("input[id=Button1]").click();
}
}

<asp:textbox id="TextBox1" onkeydown="javascript:enterEvent(event);" runat="server" xmlns:asp="#unknown">

<asp:button id="Button1" runat="server" text="Button" onclick="Button1_Click" xmlns:asp="#unknown">
 
Share this answer
 
Take all your page contains in a panel and set DefaultButton property to your submit button.
ASP.NET
<asp:panel runat="server" id="pnlSrchQuote" defaultbutton="btnSearch" xmlns:asp="#unknown">
     <!--My Controls here-->
</asp:panel>


--Amit
 
Share this answer
 
Set these properties of Form you will get your goal
ASP.NET
<form id="Form1">
       defaultbutton="SubmitButton"
       defaultfocus="TextBox1"
       runat="server"></form>
 
Share this answer
 
Refer this thread
http://stackoverflow.com/questions/268588/call-a-specific-button-onclick-event-when-the-enter-key-is-pressed-c-sharp[^]

It's quite easy and there can be many ways to do that, Google and see, u will find so many results
 
Share this answer
 
XML
<script type="text/javascript">
function FireOnClickButton() { 
document.getElementByID("Button1").click();
 
}
</script>
<asp:textbox id="TextBox1" onkeypress="FireOnClickButton();" runat="server" xmlns:asp="#unknown">

<asp:button id="Button1" runat="server" text="Button" onclick="Button1_Click" />



hopes this work for you...
 
Share this answer
 
v2
<script>
var input = document.getElementById("TextBox1");
input.addEventListener("keyup", function(event) {

if (event.keyCode === 13) {
document.getElementById("Button1").click();
}
});
</script>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900