Solution: Enter Key on Textboxes RRS feed

  • Question

  • User824228415 posted

     I have come into problems regarding adding the Entery Key functionality on the textboxes. I have found a solution and wanted to share it too.


    Example 1: this example has 4 HTML input type textboxes. every time you press enter on a textbox, the focus (cursor) will transfer to the other textbox.

    <input id="Text2" type="text" onKeyDown="if(event.keyCode==13) event.keyCode=9;"/>
    <input id="Text3" type="text" onKeyDown="if(event.keyCode==13) event.keyCode=9;"/>
    <input id="Text4" type="text" onKeyDown="if(event.keyCode==13) event.keyCode=9;"/>
    <input id="Text5" type="text" onKeyDown="if(event.keyCode==13) event.keyCode=9;"/>


    Example 2:  this example has 4 ASP type textboxes. every time you press enter on a textbox, the focus (cursor) will transfer to the other textbox.

    <asp:TextBox ID="TextBox2" runat="server" onKeyDown="if(event.keyCode==13) event.keyCode=9;"></asp:TextBox>
    <asp:TextBox ID="TextBox3" runat="server" onKeyDown="if(event.keyCode==13) event.keyCode=9;"></asp:TextBox>
    <asp:TextBox ID="TextBox4" runat="server" onKeyDown="if(event.keyCode==13) event.keyCode=9;"></asp:TextBox>
    <asp:TextBox ID="TextBox5" runat="server" onKeyDown="if(event.keyCode==13) event.keyCode=9;"></asp:TextBox>

     Example 3: this example has 1 ASP textbox and 1 ASP button. when you type something in the textbox and PRESS ENTER, the click event on the button would be triggered.

    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
    <asp:Button ID="Button1" runat="server" Text="Button" /><br />

    (code behind)
    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
    End Sub

    Protected Sub TextBox1_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
        TextBox1.Attributes.Add("onkeydown", "if(event.which || event.keyCode){if ((event.which == 13) || (event.keyCode == 13)) {document.getElementById('" + Button1.UniqueID + "').click();return false;}} else {return true}; ")
    End Sub

     Hope this helps.

    Wednesday, December 19, 2007 8:49 PM

All replies

  • User139975600 posted

    Many of these issues can be avoided by dealing with form submissions rather than button clicks:

    <asp:Button ID="btnMySubmitButton" UseSubmitBehavior="true" runat="server"

    Text="Click or press Enter" />

    Note: no click event handlers for that button. We do, however, use a true value for UseSubmitBehavior (default is TRUE)

    In your code-behind, check to see if the page was submitted (rather than initial load) and then react to that.protected void Page_Load(object sender, EventArgs e)


    if (IsPostBack)


    // enter key was pressed

    // do something here rather than using the button click event



    My two-cents.



    Thursday, December 20, 2007 3:45 PM
  • User887393078 posted


    UseSubmitBehavior gets called only when there is focus to any controls in the page. Is there any option so that when i first load the page and click on the enter should call my button's clcik event





    Monday, January 7, 2008 6:43 AM