Solved

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

Posted on 2010-09-09
5
420 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.
0
Comment
Question by:Webbo_1980
5 Comments
 
LVL 19

Expert Comment

by:Bardobrave
ID: 33634987
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();
});
0
 
LVL 12

Expert Comment

by:rajapandian_81
ID: 33635171
Set defaultbutton property of page.

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

OR

Page.Form.DefaultButton = btnSubmit.UniqueID;
0
 
LVL 25

Expert Comment

by:Luis Pérez
ID: 33635203
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?
0
 

Author Comment

by:Webbo_1980
ID: 33635462
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
0
 
LVL 12

Accepted Solution

by:
rajapandian_81 earned 500 total points
ID: 33635626
Webbo_1980:

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.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

Suggested Solutions

Title # Comments Views Activity
Alert after MVC form submitted 1 22
C# MVC Insert Multiple Row into DB 2 30
Insert Button on a table 16 36
Character counter breaks after adding EmojiOne Area 4 19
This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

789 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