Image Button onkeypress

C

crferguson

I know this has been beaten a hundred different ways, but I've tried
them all and I'm stuck. I have a web control on a web form. The web
control is for logging in and all it has are two textboxes (username
and password) and an imagebutton Submit button. The main form also
has a handful of image buttons. I need the Submit imagebutton to fire
when the user hits the Enter key. Right now when Enter is pressed,
the first button on the page fires instead, which happens to be the
Home button, and the user is taken from the Login page to the Home
page.

I know this behavior is simply another "feature" of ASP.NET in that
all asp buttons are rendered as html submit buttons, so the first one
on the page is fired on Enter. But, I also know that hundreds of
programmers have worked around this through a few lines of
javascript. Every method I've tried just results in java syntax
errors, odd behavior such as "object expected" java errors, no
behavior at all. Here are the scripts I've tried so far to no
avail....

======================
Method 1:
<script language="javascript">
function SubmitForm(btnName) {
if ( event.keyCode == 13 ) {
event.returnValue=false;
var btn = document.forms[0][btnName];
btn.click();
}
}
</script>

then add an onkeypress call to SubmitForm passing the webcontrol's
ibSubmit button clientID name.

======================
Method2:
function document.onkeydown() {
if ( event.keyCode == 13 ) {
Form1.WebControlForm1_ibSubmit.click();
event.returnValue=false;
}
}

======================
Method3:
<script>
function submitForm()
{
if (event.keyCode == 13)
{
Form1.submit();
}
}
</script>

then add an onkeydown call in the main page's Body tag or in the
password textbox's attributes

======================
Method4:
<script>
function clickButton() {
if (event.keyCode == 13) {
document.form.button1.click();
return false;
}
}
</script>

then add an onkeydown call in the main page's Body tag or in the
password textbox's attributes

======================
Method5:
<script>
function catchEnter(){
if(event.keyCode == 13)
__doPostBack('<%=ibSubmit.ClientID%>','');
return false;
else
return true;
}
</script>

then add an onkeypress call in the main page's Body tag or in the
password textbox's attributes. Here I've tried dynamically
determining the imagebutton's client id as above, and I've hardcoded
the resulting client ID directly into that function.

======================

Any help would be greatly appreciated. I've been looknig at this too
long and can't figure out why nothing is working. I know I can use
different types of buttons, but this spec is company determined and
needs to be an imagebutton control due to reasons unrelated to this
post.

Thank all!!

Cory
 
S

Stump

Did you ever find an answer?

I have a similar problem in that my javascript blocks are not being triggered from any controls on the asp page.
I know this has been beaten a hundred different ways, but I've tried
them all and I'm stuck. I have a web control on a web form. The web
control is for logging in and all it has are two textboxes (username
and password) and an imagebutton Submit button. The main form also
has a handful of image buttons. I need the Submit imagebutton to fire
when the user hits the Enter key. Right now when Enter is pressed,
the first button on the page fires instead, which happens to be the
Home button, and the user is taken from the Login page to the Home
page.

I know this behavior is simply another "feature" of ASP.NET in that
all asp buttons are rendered as html submit buttons, so the first one
on the page is fired on Enter. But, I also know that hundreds of
programmers have worked around this through a few lines of
javascript. Every method I've tried just results in java syntax
errors, odd behavior such as "object expected" java errors, no
behavior at all. Here are the scripts I've tried so far to no
avail....

======================
Method 1:
<script language="javascript">
function SubmitForm(btnName) {
if ( event.keyCode == 13 ) {
event.returnValue=false;
var btn = document.forms[0][btnName];
btn.click();
}
}
</script>

then add an onkeypress call to SubmitForm passing the webcontrol's
ibSubmit button clientID name.

======================
Method2:
function document.onkeydown() {
if ( event.keyCode == 13 ) {
Form1.WebControlForm1_ibSubmit.click();
event.returnValue=false;
}
}

======================
Method3:
<script>
function submitForm()
{
if (event.keyCode == 13)
{
Form1.submit();
}
}
</script>

then add an onkeydown call in the main page's Body tag or in the
password textbox's attributes

======================
Method4:
<script>
function clickButton() {
if (event.keyCode == 13) {
document.form.button1.click();
return false;
}
}
</script>

then add an onkeydown call in the main page's Body tag or in the
password textbox's attributes

======================
Method5:
<script>
function catchEnter(){
if(event.keyCode == 13)
__doPostBack('<%=ibSubmit.ClientID%>','');
return false;
else
return true;
}
</script>

then add an onkeypress call in the main page's Body tag or in the
password textbox's attributes. Here I've tried dynamically
determining the imagebutton's client id as above, and I've hardcoded
the resulting client ID directly into that function.

======================

Any help would be greatly appreciated. I've been looknig at this too
long and can't figure out why nothing is working. I know I can use
different types of buttons, but this spec is company determined and
needs to be an imagebutton control due to reasons unrelated to this
post.

Thank all!!

Cory

Submitted via EggHeadCafe - Software Developer Portal of Choice
Book Review: Excel 2010 - The Missing Manual [OReilly]
http://www.eggheadcafe.com/tutorial...w-excel-2010--the-missing-manual-oreilly.aspx
 

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

Forum statistics

Threads
474,056
Messages
2,570,443
Members
47,089
Latest member
Bobby2025b

Latest Threads

Top