javascript doesn't work with my button

G

Guest

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
 
M

Marina Levit [MVP]

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.
 
G

Guest

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)
break;
i = (i + 1) % field.form.elements.length;
field.form.elements.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
 
M

Marina Levit [MVP]

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.
 
G

Guest

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)
break;
i = (i + 1) % field.form.elements.length;
field.form.elements.focus();
return false;
}
else
return true;
}



Thanks again,
regards,
CharlesA
 
B

bruce barker \(sqlwork.com\)

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)
 
M

Marina Levit [MVP]

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.
 
R

Russell

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.
 

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
473,768
Messages
2,569,574
Members
45,049
Latest member
Allen00Reed

Latest Threads

Top