We help IT Professionals succeed at work.

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

Webbo_1980
Webbo_1980 asked
on
452 Views
Last Modified: 2012-05-10
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.

Thanks
Webbo

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.
Comment
Watch Question

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)
    $("#myNETButton").click();
});
Set defaultbutton property of page.

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

OR

Page.Form.DefaultButton = btnSubmit.UniqueID;
Luis PĂ©rezSoftware Architect in .Net
CERTIFIED EXPERT

Commented:
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?

Author

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
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.