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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SharePoint 2013 List with Ratings 6 32
Host asp.net pages 5 25
ngStyle with an input variable doesn't work 14 30
ASP.NET 5 Templates 2 65
In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
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…

911 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

20 Experts available now in Live!

Get 1:1 Help Now