By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,718 Members | 1,840 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,718 IT Pros & Developers. It's quick & easy.

javascript doesn't work with my button

P: n/a
Hi folks,
first off, I'm using .Net framework 1.1 with ASP.net and C#

I'm trying to do something very simple...but I can't figure it out

I have an <asp:button runat="server"> in the test version of my app that is
essentially a backdoor for changing the class of user inside the application
on the fly....it has a textbox next to it

you can type 1, 2, 3, 4 etc.. in the text box and when you click the button
it changes the whole context of the application, the screen have more or less
tabs etc..
this is the hard part...the easy part is that I never ever want this button
to be fired with 'Enter' or 'Return' being pressed or in fact any key, I want
this button to be only mouse-clickable (because our tester is pressing keys
madly to test the app and constantly fires this button, and we protest that
this backdoor will not be available in the release version) but to no avail.

....I can't figure out a property that will do this so I've resorted to
Javascript without sucess (getting onKeypress examples from the web, which
check for Enter and return's codes)

the trouble is the event never fires...

is this to do with the fact that it's a runat Server and it nevers sees the
javaScript, or is it that you can't trap keystrokes for buttons only for
textboxes etc... if anyone has solved this seemingly trivial but intractable
problem I would greatly appreciate their help

Thanks in advance and Regards,
CharlesA

Apr 27 '06 #1
Share this Question
Share on Google+
7 Replies


P: n/a
Because this all happens on the client, you do need javascript to do this.

Since you didn't actually provide the code you are trying to use, it's hard
to say what your problem is.

"CharlesA" <Ch******@discussions.microsoft.com> wrote in message
news:3E**********************************@microsof t.com...
Hi folks,
first off, I'm using .Net framework 1.1 with ASP.net and C#

I'm trying to do something very simple...but I can't figure it out

I have an <asp:button runat="server"> in the test version of my app that
is
essentially a backdoor for changing the class of user inside the
application
on the fly....it has a textbox next to it

you can type 1, 2, 3, 4 etc.. in the text box and when you click the
button
it changes the whole context of the application, the screen have more or
less
tabs etc..
this is the hard part...the easy part is that I never ever want this
button
to be fired with 'Enter' or 'Return' being pressed or in fact any key, I
want
this button to be only mouse-clickable (because our tester is pressing
keys
madly to test the app and constantly fires this button, and we protest
that
this backdoor will not be available in the release version) but to no
avail.

...I can't figure out a property that will do this so I've resorted to
Javascript without sucess (getting onKeypress examples from the web, which
check for Enter and return's codes)

the trouble is the event never fires...

is this to do with the fact that it's a runat Server and it nevers sees
the
javaScript, or is it that you can't trap keystrokes for buttons only for
textboxes etc... if anyone has solved this seemingly trivial but
intractable
problem I would greatly appreciate their help

Thanks in advance and Regards,
CharlesA

Apr 27 '06 #2

P: n/a
Thanks Marina....
that's a good point, however I did mention that the event wasn't firing at
all.
so the problem starts before the content of the javascript

I knew this because I'd put an alert() in there and it never fired off...

this was the javaScript
function handleEnter (field, event) {

altert("You are in event code");
var keyCode = event.keyCode ? event.keyCode : event.which ? event.which :
event.charCode;
if (keyCode == 13) {
var i;
for (i = 0; i < field.form.elements.length; i++)
if (field == field.form.elements[i])
break;
i = (i + 1) % field.form.elements.length;
field.form.elements[i].focus();
return false;
}
else
return true;
}

and this was the way I attached it to the button

btnRole.Attributes.Add("onKeyPress","return handleEnter(this, event);");

then I thought , hey presumably the button doesn't have an onKeypress event

so I did this:

btnRole.Attributes.Add("onClick","return handleEnter(this, event);");

and then it occured to me hey maybe onClick doesn't trap any key information

I'm flummoxed

Regards and thanks,
CharlesA
Apr 27 '06 #3

P: n/a
Well, for one your call to alert is actually spelled 'altert', so that would
never fire. In fact, it should cause a javascript error.

Be sure you have your browser set to show all javascript errors and to
enable debugging.

"CharlesA" <Ch******@discussions.microsoft.com> wrote in message
news:05**********************************@microsof t.com...
Thanks Marina....
that's a good point, however I did mention that the event wasn't firing at
all.
so the problem starts before the content of the javascript

I knew this because I'd put an alert() in there and it never fired off...

this was the javaScript
function handleEnter (field, event) {

altert("You are in event code");
var keyCode = event.keyCode ? event.keyCode : event.which ? event.which :
event.charCode;
if (keyCode == 13) {
var i;
for (i = 0; i < field.form.elements.length; i++)
if (field == field.form.elements[i])
break;
i = (i + 1) % field.form.elements.length;
field.form.elements[i].focus();
return false;
}
else
return true;
}

and this was the way I attached it to the button

btnRole.Attributes.Add("onKeyPress","return handleEnter(this, event);");

then I thought , hey presumably the button doesn't have an onKeypress
event

so I did this:

btnRole.Attributes.Add("onClick","return handleEnter(this, event);");

and then it occured to me hey maybe onClick doesn't trap any key
information

I'm flummoxed

Regards and thanks,
CharlesA

Apr 27 '06 #4

P: n/a
oooops...well spotted...
I've fixed that, but still the event doesn't fire and in fact you're right
there is that symbol at the lower left hand side of the status bar that shows
a javascript error

so now I add the code to the button like so:
btnRole.Attributes.Add("onClick","return handleEnter(this, event);");

and the javascript is now this...
function handleEnter (field, event) {

alert("You are in event code");
var keyCode = event.keyCode ? event.keyCode : event.which ? event.which :
event.charCode;
if (keyCode == 13) {
var i;
for (i = 0; i < field.form.elements.length; i++)
if (field == field.form.elements[i])
break;
i = (i + 1) % field.form.elements.length;
field.form.elements[i].focus();
return false;
}
else
return true;
}

Thanks again,
regards,
CharlesA
Apr 27 '06 #5

P: n/a
the browsers default behavior is to submit a form on enter. it picks the
first submit (or the one with the focus) to send as the "clicked" button.
catching the keypress on the button will ony catch a return if the button
has the focus when the enter is pressed. you need to catch keypress at the
body level.

<body onkeypress="handleEnter(event)">

-- bruce (sqlwork.com)

"CharlesA" <Ch******@discussions.microsoft.com> wrote in message
news:05**********************************@microsof t.com...
Thanks Marina....
that's a good point, however I did mention that the event wasn't firing at
all.
so the problem starts before the content of the javascript

I knew this because I'd put an alert() in there and it never fired off...

this was the javaScript
function handleEnter (field, event) {

altert("You are in event code");
var keyCode = event.keyCode ? event.keyCode : event.which ? event.which :
event.charCode;
if (keyCode == 13) {
var i;
for (i = 0; i < field.form.elements.length; i++)
if (field == field.form.elements[i])
break;
i = (i + 1) % field.form.elements.length;
field.form.elements[i].focus();
return false;
}
else
return true;
}

and this was the way I attached it to the button

btnRole.Attributes.Add("onKeyPress","return handleEnter(this, event);");

then I thought , hey presumably the button doesn't have an onKeypress
event

so I did this:

btnRole.Attributes.Add("onClick","return handleEnter(this, event);");

and then it occured to me hey maybe onClick doesn't trap any key
information

I'm flummoxed

Regards and thanks,
CharlesA

Apr 27 '06 #6

P: n/a
If there is a javascript error, then obviously something is wrong, and that
is why the code isn't working.

Like I said, you need to change your setting to get a notification about
every error, and then look at what the error is and fix it. You page should
not have any errors in it. I'm not sure why you didn't explore further to
eliminate all javascript errors on your page?

Additionally, it is best to just start out with a simple function that only
has an 'alert' in it. After you get that working, then you add on to it.

"CharlesA" <Ch******@discussions.microsoft.com> wrote in message
news:69**********************************@microsof t.com...
oooops...well spotted...
I've fixed that, but still the event doesn't fire and in fact you're right
there is that symbol at the lower left hand side of the status bar that
shows
a javascript error

so now I add the code to the button like so:
btnRole.Attributes.Add("onClick","return handleEnter(this, event);");

and the javascript is now this...
function handleEnter (field, event) {

alert("You are in event code");
var keyCode = event.keyCode ? event.keyCode : event.which ? event.which :
event.charCode;
if (keyCode == 13) {
var i;
for (i = 0; i < field.form.elements.length; i++)
if (field == field.form.elements[i])
break;
i = (i + 1) % field.form.elements.length;
field.form.elements[i].focus();
return false;
}
else
return true;
}

Thanks again,
regards,
CharlesA

Apr 27 '06 #7

P: n/a
An ASP button generates an "input type=submit" button, subject to "the
browsers default behavior which is to submit a form on enter. it picks
the first submit" as noted above.

You might try a LinkButton, which is not subject to this behavior
because it's really a hyperlink that fires JavaScript to submit the
form.

Apr 28 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.