Textbox, button and Enter on keyboard

D

DDK

Question (using C# and ASP.NET),

I have a textbox and a buttonClick on my asp.net page. I would
like be able to hit the Enter button on the keyboard and have it do the
same thing as the buttonClick. In other words how do you get Enter button
on the key board to perform the same operation as a button click event.

Any help would be great,
Thanks

D.
 
V

Vidar Petursson

Hi

Basic example... simply say what submitbutton should be clicked onkeydown
event
I think this will work in netscape also but havent tested it....

<html>
<head>
<SCRIPT>
function submitOnEnter(submitBtn) {
var keyCode = document.all ? event.keyCode :evt.which ? evt.which :
evt.keyCode ? evt.keyCode : evt.charcode;
if (keyCode == 13) submitBnt.click();
}
</script>
</head>
<body>
<form >
<input type="text" name="myField"
onkeydown="submitOnEnter(this.form.submitBtn1)">
<input type="submit" name="submitBtn1" value="submit1">
<input type="text" name="myField"
onkeydown="submitOnEnter(this.form.submitBtn2)">
<input type="submit" name="submitBtn2" value="submit2">
</form>
</body>
</html>

So if sumbitOnEnter-script is registered and you want to set some field to
use it
myInput.Attributes.Add("onkeydown","submitOnEnter(this.form." + BUTTONNAME +
")");
Where BUTTONNAME is the button (surprise ;) ) to click

--
Best Regards
Vidar Petursson
==============================
Microsoft Internet Client & Controls MVP
==============================
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top