Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win


Enter on keyboard for Next Button

Posted on 2004-10-26
Medium Priority
Last Modified: 2012-06-21
I have a Next Button on my form. Currently I'm able to click on the mouse and it takes me to a different page.

I want to be able to hit on the Enter key on a keyboard for my Next Buttona and it will takes me to a different page. How do I do this on VS.NET environment withouth using JavaScripts. Please Help.
Question by:missqti
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
  • 2
  • +2

Expert Comment

ID: 12414313
If you just need a "Next" button and not a "Submit" button, then just create the button like normal and make it say "Next" - hitting Enter will automatically submit it and you can set your form action to the next page.


Author Comment

ID: 12414624
Nope, it didn't work that way. I have to force to click on the mouse first I can't use the Enter Key. Is there a work around for this?

Here is the example of my code

Private Sub btnNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNext.Click
      Dim url = "http://www.Experts_Exchange"

End Sub
LVL 18

Expert Comment

ID: 12414970
In HTML, with a form, when a form element has focus and the ENTER key is pressed, the first <input type="submit"> (Or, I believe <button type="submit">) button is triggered.

Do whatever you have to do in (*sudder*) .NET to get that tag in the HTML.
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Author Comment

ID: 12415348
Sorry Arantius I don't understand what you are telling me.

nan1217 I figured out the problem. Currently there is only one Next Button on the page, no text box at all. If i hit the tab key twice. It will takes me to the Next Button and I was able to hit the Enter Key.

But my question is how do i set the focus to Next Button withouth hitting the tab key twice.


Expert Comment

ID: 12415381
missqti, I'm with arantius and we're trying to say the same thing in different ways.

Whenever you define your form, you put a "submit" button in it, and when the user hits "enter" that button is submitted.  

So, let's say your form could look like this....

<form method="post" action="mynextpage.aspx">
<input type="text" name="sometextbox">
<input type=submit value="Next">

When the user is in the text box and hits enter, the form will submit and go to mynextpage.aspx

Expert Comment

ID: 12418137
I may be wrong, but I think you have to use clientside script.  Set the body onload event to form.nextbutton.focus()


<body onload="frmMain.btnNext.focus();">

Expert Comment

ID: 12418157
You can also set the tabindex for the button to 0 so it will be the first form item that gets focus when you hit Tab.

Expert Comment

ID: 12419554

A correct way to do it would be to assign the Enter key on the page to simulate Next button click, via event handling.

Note that event handling is a bit tricky, as it differs among browsers, so in order for that to work on Mozilla as well as IE, you'd have to have some wrapper funcs first.

Still here is how it works with IE:

<script language="Javascript">

function onKey()
      if (window.event.keyCode == 13) // Enter pressed?

document.attachEvent("onkeydown",onKey); // react to pressing anywhere in the page


<button id="nextbutt">next button</button>

Author Comment

ID: 12423479
genXerator: set tabindex to 0(zero) is not working for my case.

gnudiff: I'm trying NOT to use JavaScripts. I want to use the full asp.net/vb.net for this. I'm going to try something else. Will post the answer if it works.

Author Comment

ID: 12423929
I found the answer myself no points to anyone right now. The points will go to whoever figure out how to set the default to the button without needing the text box there.

Ok here is the code that work only if you have a textbox. My case I don't need a textbox and still having a problem how to figure it out.

You DO need to write a JavaScripts for this .net application. Put the scripts in between of your head tag

     Below Script goes here....

 <script language="javascript">
    function KeyDownHandler(btn)
        // process only the Enter key
        if (event.keyCode == 13)
            // cancel the default submit
            event.cancel = true;
            // submit the form by programmatically clicking the specified button

Now, in the input controls declaration you just have to call this function when a button is pressed, by handling the onKeyPressed client-side event, and pass a reference to the default button:

<asp:Button Runat="server" ID="DefButton" />
<asp:TextBox Runat="server" ID="FirstName" onKeyDown= "KeyDownHandler(DefButton)" />

Accepted Solution

gnudiff earned 500 total points
ID: 12424451

You can't control browser to that extent using server-side technology.

Your script does exactly what I said and provided.

You can't run it at server because the server just passes data to client and only receives it, when user specifically REQUESTs so.

The fact that button does it at server has nothing whatsoever to do with Javascript. It still runs browser-side.

To do it without textbox, you simply do:

<asp:Button Runat="server" ID="DefButton" onKeyDown= "KeyDownHandler(DefButton)"/>

and you will get basically same thing which I suggested. :P

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Australian government abolished Visa 457 earlier this April and this article describes how this decision might affect Australian IT scene and IT experts.
CTAs encourage people to do something specific to show interest in your company, product or service. Keep reading to learn why CTAs should always be thought of as extremely important, albeit small, sections of websites.
This tutorial demonstrates how to identify and create boundary or building outlines in Google Maps. In this example, I outline the boundaries of an enclosed skatepark within a community park.  Login to your Google Account, then  Google for "Google M…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses

610 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question