Click here to Skip to main content
14,217,717 members

Capturing the Enter key to cause a button click

Rate this:
4.92 (38 votes)
Please Sign up or sign in to vote.
4.92 (38 votes)
19 Jan 2007CPOL
Have you ever wanted to trap the Enter key press event in ASP.NET and have it click a button for you? This article describes two options for capturing the Enter key press event and then clicking a button. One solution uses JavaScript, the other uses a Panel control.

Sample screenshot


Have you ever wanted to trap the Enter key press event in ASP.NET and have it click a button for you? This article describes two options for capturing the Enter key press event and then clicking a button. One solution uses JavaScript, the other uses a Panel control.


Most people like things to work a certain way. Consider a search engine text box. After you type in your search text, most people just want to push the Enter key and have the search start. It is a pain when you have to grab the mouse and move the cursor to the Search button and click it. The article describes two solutions to this problem.

The problem

Web pages have text boxes, text areas, etc., where users enter text. Often, after the text is entered, the user needs to click a button. It may be a search button, or a submit button, or some other sort of button. The problem is how to trap the Enter key press event so that the correct button can be clicked.

The solution

Let's explore two solutions to this problem. First, let us look at using JavaScript. With this solution, each textbox needs to have some JavaScript attached to the onKeyPress JavaScript event. I usually do this in the pageload method in the code-behind.

//Add the javascript so we know where we want the enter key press to go
if (!IsPostBack)
                   "doClick('" + btnSearch.ClientID + "',event)");
                  "doClick('" + btnSearch.ClientID + "',event)");
                  "doClick('" + btnSearch.ClientID + "',event)");
                  "doClick('" + btnSearch.ClientID + "',event)");
              "doClick('" + btnSearch.ClientID + "',event)");
               "doClick('" + btnSearch.ClientID + "',event)");
                 "doClick('" + btnSearch.ClientID + "',event)");

Note: You pass into the JavaScript method the button's ClientID so that the JavaScript method can find the button and call its Click method.

Next, we need a JavaScript method called doClick in the ASP.NET form.

<SCRIPT type=text/javascript>
    function doClick(buttonName,e)
        //the purpose of this function is to allow the enter key to 
        //point to the correct button to click.
        var key;

              key = window.event.keyCode;     //IE
              key = e.which;     //firefox
        if (key == 13)
            //Get the button the user wants to have clicked
            var btn = document.getElementById(buttonName);
            if (btn != null)
            { //If we find the button click it
                event.keyCode = 0

The next solution is using a Panel control. In this case, the panel is doing all the work.

<asp:Panel ID="panSearch" runat="server" 

       DefaultButton="btnSearch2" Width="100%" >
    <table width="100%">
     <td>First Name</td>
     <td ><asp:TextBox ID="txtboxFirstName2" 

           runat="server" ></asp:TextBox></td>

Notice that the Panel tag has a property called DefaultButton. You set this property to the button ID of the button you want to be clicked on an Enter key press event. So any text box inside of the Panel will direct its Enter key press to the button set in the DefaultButton property of the Panel.

Which one is better?

I suppose we could spend a lot of time arguing about which method is better. Some people like JavaScript, so that might be the better one. Others hate JavaScript and prefer writing less code, so they would choose the Panel solution. One thing I would note is that if you have several buttons and need to have different text boxes click different buttons, then you have to go with the JavaScript solution since it gives you that flexibility. I guess I will let you decide which solution best fits your needs.


Hopefully, this article demonstrates two useful techniques to capture the Enter key press event and directing it to click the correct button. Let me know which method you prefer.


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


About the Author

Software Developer (Senior)
United States United States
I started my programmer career over 21 years ago doing COBOL and SAS on a MVS mainframe. It didn't take long for me to move into windows programming. I started my windows programming in Delphi (Pascal) with a Microsoft SQL server back end. I started working with when the beta 2 came out in 2001. After spending most of my programming life as a windows programmer I started to check out in 2004. I achieved my in April 2005. I have done a lot of MS SQL database stuff. I have a lot of experience with Window Service and Web services as well. I spent three years as a consultant programing in C#. I really enjoyed it and found the switch between and C# to be mostly syntax. In my current position I am programming in C# working on WPF and MSSql database stuff. Lately I have been using VS2015.

On a personal note I am a born again Christian, if anyone has any questions about what it means to have a right relationship with God or if you have questions about who Jesus Christ is, send me an e-mail. ben.kubicek[at]netzero[dot]com You need to replace the [at] with @ and [dot] with . for the email to work. My relationship with God gives purpose and meaning to my life.

Comments and Discussions

Generaljavascript newbie help [modified] Pin
enhesive13-May-08 5:50
memberenhesive13-May-08 5:50 
GeneralRe: javascript newbie help Pin
kubben13-May-08 8:58
memberkubben13-May-08 8:58 
GeneralRe: javascript newbie help Pin
enhesive13-May-08 9:14
memberenhesive13-May-08 9:14 
GeneralRe: javascript newbie help Pin
kubben13-May-08 9:22
memberkubben13-May-08 9:22 
GeneralRe: javascript newbie help Pin
enhesive13-May-08 10:43
memberenhesive13-May-08 10:43 
GeneralRe: javascript newbie help Pin
kubben13-May-08 14:14
memberkubben13-May-08 14:14 
QuestionHow about Pocket IE Pin
mymacryan14-Nov-07 10:20
membermymacryan14-Nov-07 10:20 
AnswerRe: How about Pocket IE Pin
kubben14-Nov-07 12:45
memberkubben14-Nov-07 12:45 
I havn't worked with pocket IE. Are you trying the javascript method or the panel method? I would think the panel method would work in pocket IE.

GeneralRe: How about Pocket IE Pin
mymacryan15-Nov-07 4:14
membermymacryan15-Nov-07 4:14 
GeneralRe: How about Pocket IE Pin
kubben15-Nov-07 4:51
memberkubben15-Nov-07 4:51 
stixoffire15-Jun-07 23:21
memberstixoffire15-Jun-07 23:21 
GeneralRe: EASIER CODE Pin
kubben16-Jun-07 0:43
memberkubben16-Jun-07 0:43 
stixoffire16-Jun-07 23:37
memberstixoffire16-Jun-07 23:37 
GeneralRe: EASIER CODE Pin
kubben17-Jun-07 3:09
memberkubben17-Jun-07 3:09 
stixoffire17-Jun-07 18:13
memberstixoffire17-Jun-07 18:13 
GeneralRe: EASIER CODE Pin
KowDot29-Jun-07 16:56
memberKowDot29-Jun-07 16:56 
GeneralRe: EASIER CODE Pin
stixoffire2-Jul-07 8:31
memberstixoffire2-Jul-07 8:31 
GeneralCapture Enter Key Press for log out link button Pin
Vani Kulkarni21-May-07 2:52
professionalVani Kulkarni21-May-07 2:52 
GeneralRe: Capture Enter Key Press for log out link button Pin
kubben21-May-07 3:06
memberkubben21-May-07 3:06 
AnswerRe: Capture Enter Key Press for log out link button Pin
stixoffire15-Jun-07 23:22
memberstixoffire15-Jun-07 23:22 
GeneralThank you Pin
TheGeneral6915-May-07 7:42
memberTheGeneral6915-May-07 7:42 
GeneralRe: Thank you Pin
kubben15-May-07 7:53
memberkubben15-May-07 7:53 
GeneralRe: Thank you Pin
Berend Engelbrecht15-Sep-07 9:34
memberBerend Engelbrecht15-Sep-07 9:34 
GeneralRe: Thank you Pin
kubben15-Sep-07 14:04
memberkubben15-Sep-07 14:04 
GeneralJavascript method doesn't work if you are selecting from autofill Pin
JohnyWhite26-Apr-07 18:10
memberJohnyWhite26-Apr-07 18:10 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Posted 19 Jan 2007


49 bookmarked