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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Executing function when pressing ENTER 1

Status
Not open for further replies.

tvbruwae

Programmer
Aug 9, 2001
224
EU
I created a search page with several textboxes and a search button that calls a function when clicked. However, I'd like the search function to be executed when pressing ENTER as well. Now, the only thing I get is a script error ("object expected"). There are some techniques to use the ENTER button to submit a HTML form, but I can't apply them to these DTC's. How can you assign a javascript or vbscript function to these DTC's (for example onkeypress)?
 
As far as I know, if you press ENTER on a ASP page it goes to the first input button with an image.
so far i tried 2 things in catching ENTER.

1. I put a HTML input button and invisible DTC with the same name on the page. DTC button onclick event contains all the code:
Sub btnEnter_onclick()
...your code...
end sub

The INPUT button calls that event:

<input id=&quot;btnEnter&quot; onclick=&quot;thisPage._fireEvent('btnEnter', 'onclick'); return false;&quot; type=&quot;image&quot; alt=&quot;enter&quot; src=&quot;images/blah.gif&quot; border=&quot;0&quot; name=&quot;enter&quot; WIDTH=&quot;69&quot; HEIGHT=&quot;21&quot;>

2. You can catch the onkeypress event (for example) of one of your DTC textboxes.
Put advise method in thisPage_onenter function (it's a server side function)

Sub thisPage_onenter()
txtName.advise &quot;onkeypress&quot;, &quot;nofunc&quot;
end sub

Then catch the event on client side and cancel server round trip (you have to do this, because if you don't cancel it, it will call function nofunc, that in this case doesn't exist).

function thisPage_onbeforeserverevent(obj,event){
if (obj==&quot;txtName&quot;){
if (event==&quot;onkeypress&quot;){
if (window.event.keyCode==&quot;13&quot;){
thisPage._fireEvent(&quot;btnEnter&quot;,&quot;onclick&quot;)
}
else
document.thisForm.txtName.value+=String.fromCharCode(window.event.keyCode);
thisPage.cancelEvent=true;
}
}
}

DTC button contains your code:
Sub btnEnter_onclick()
...your code...
end sub

Hope that helps you. The second example however works only in IE browser, I don't know what to do in Netscape.

If anyone knows a better way how to catch ENTER, please post it, i'd really like to know.
 
I tried the first one, works fine. Thanks for posting!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top