• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 444
  • Last Modified:

Is there a way to simulate an asp.net button.click event to fire when enter is pressed "anywhere" on a web page

Is there a way to simulate an asp.net button.click event to fire when enter is pressed on the screen.

As this is in theory a relativly small amount code involved may i request a working exmaple/or code samples.


NB: settng focus on the button isnt an option as the page this is used on is highly interactive, and i'm guessing js will need to be used.
1 Solution
Well... I'm not sure if this is exactly what you need, but you can attack via JavaScript an onKeyPress event to <BODY> tag on your page, and when the pressed key is "INTRO" you can fire your click button.

If you use jquery (wich I highly recommend for web development) the attach event will be something like:

$("body").keypress(function (event) {
  if (event.keycode == 13)
Set defaultbutton property of page.

<form id="form1" runat="server" defaultbutton="btnSubmit">


Page.Form.DefaultButton = btnSubmit.UniqueID;
Luis PérezSoftware Architect in .NetCommented:
Is it a client button or an ASP.net server button control? Do you want only the client javascript code of the button (if it has someone) to be executed or also the server button's code?
Webbo_1980Author Commented:
hi all few questions...

rajapandian_81:if i set this, will all other postbacks work as normal?

RolandDeschain: to anwser your question it an asp.net server side control, and i need the server side code to run not the js

Normally defaultbutton will be "first button" available in the page. Whenever enter is pressed from keyboard that first button event will be triggered. My code will change the default button to "user defined button".

It will not affect any other things.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now