Textbox, button and Enter on keyboard

  • Thread starter Thread starter DDK
  • Start date Start date
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.
 
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
==============================
 
Back
Top