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
395 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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
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…

759 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now