Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

suspend the enter key to submit form at diff browser

Status
Not open for further replies.

smallpotato

IS-IT--Management
Jun 6, 2002
14
0
0
HK
I'm using the following javascipt to "onkeydown" event at the controls to disable pressing Enter key to submit the form in the asp.net 1.1 pages

---------------------
if (document.all) {
if (event.keyCode == 13) {
event.returnValue=false;
event.cancel = true;
return false;
}
}
else if (document.getElementById) {
if (event.which == 13) {
event.returnValue=false;
event.cancel = true;
return false;
}
}
else if(document.layers) {
if(event.which == 13) {
event.returnValue=false;
event.cancel = true;
return false;
}
};

---------------------

It works fine at IE but fail at FireFox & Netscape. :(
Do you have any idea to fix it? or any other suggestion to make it?

Thx in advance.

 
"event" as a global variable doesn't exist outside of IE, so you'd need to pick up on the event data passed into the function (and choose between that and event).

You also shouldn't need all of that old browser-detection - unless you really need to support NN 4 (which I seriously hope you don't).

If you're still stuck, post the function as a whole (including definition with any params, etc), and detail how/where it is called from.

Hope this helps,
Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Thx for ur kindly reply, Dan.

I use the "Attributes.Add" to stick the above script to the "onClick" event of the textbox and no more. As you say, the "event" global variable is not available except IE, how can I detect the browser so as to suspend the enter key press for form submission?

Thx a lot!!

smallpotato
 
My Previous Post said:
If you're still stuck, post the function as a whole (including definition with any params, etc), and detail how/where it is called from.

Coedit Limited - Delivering standards compliant, accessible web solutions

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
I create the following function to handle :

Private Sub SkipEnterTbx(ByVal oControl As Control)
Dim strJScript As String

strJScript = String.Concat("if (document.all) {if (event.keyCode == 13) {event.returnValue=false; event.cancel = true; return false;}} ", _
"else if (document.getElementById) {if (event.which == 13) {event.returnValue=false; event.cancel = true; return false;}} ", _
"else if(document.layers) {if(event.which == 13) {event.returnValue=false; event.cancel = true; return false;}};")

For Each frmCtrl as Control In oControl.Controls
If TypeOf frmCtrl Is TextBox Then
CType(frmCtrl, TextBox).Attributes.Add("onkeydown", strJScript)
End If
Next
End Sub


Then, at the user control, add "SkipEnterTbx(Me.Page)" to suspend the enter key to submit the form when page load.

Is it a good way to do?

 
Hmm... This is getting messy. As a client-side person, I'd certainly not do it that way - I'd add the "onkeydown" handler in an external script file, not using .Net. If this option isn't available to you, then the next best thing is puttinga script section on yor page. Are these options available to you?

Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top