I came up with an extensible version of this. In a web application I'm now
working on, we have multiple pages that have form fields on them, some of
which we want to click certain buttons on that form when the ENTER key is
pressed. What I did was to come up with a JavaScript that could be kept in a
User Control common to all of the pages. When we want a certain form field
to click a certain button, we add a case to the switch statement in the main
function, and add an "onfocus" and "onblur" JavaScript event handler to the
form field. The script looks like this:
<script language=javascript type=text/javascript>
var hasFocus = null; // Used to indicate which control has the focus at
any given time
function setFocus(obj) //sets the hasFocus variable to the element which
has received the focus (via onfocus event)
{
hasFocus = obj;
}
function loseFocus() // clears the hasFocus variable when the element loses
focus (via onblur event)
{
hasFocus = null;
}
document.onkeypress =
function checkKeyPress(e)
{
if(!e)e = window.event;
var key = (typeof e.which == 'number')?e.which:e.keyCode;
if(key == 13) {handleKP();
return (false);}
}
function handleKP()
{
if (hasFocus == null) return (false);
switch (hasFocus.id)
{
case "AirportSearch_RegularSearch_txtAirportID":
case "AirportSearch_RegularSearch_txtAirportName":
case "AirportSearch_RegularSearch_txtAirportCity":
document.getElementById('AirportSearch_RegularSear ch_btnSearchSubmit').click
();
break;
case "AirportSearch_DistanceSearch_txtAirportCity":
case "AirportSearch_DistanceSearch_txtAirportDistan ce":
document.getElementById('AirportSearch_DistanceSea rch_btnSearchSubmit').clic
k();
break;
case "AirportSearch_AdvancedSearch_txtAirportID":
case "AirportSearch_AdvancedSearch_txtAirportName":
case "AirportSearch_AdvancedSearch_txtAirportCity":
document.getElementById('AirportSearch_AdvancedSea rch_btnSearchSubmit').clic
k();
break;
case "HeaderLogin1_txtPassword":
document.getElementById('HeaderLogin1_ImageButtonL oginGo').click();
break;
case "HeaderLogin1_txtAirportID":
document.getElementById('HeaderLogin1_ImageButtonA irportGo').click();
break;
case "Login1_txtUserPassword":
document.getElementById('Login1_btnLoginSubmit').c lick();
break;
}
return (false);
}
</script>
Here is a sample of an element (HtmlControl on the server) that would be
handled by this script:
<input name="HeaderLogin1:txtPassword" type="text"
id="HeaderLogin1_txtPassword" class="txtLoginBox"
onblur="loseFocus()" onfocus="setFocus(this)" style="width:80px;" />
--
HTH,
Kevin Spencer
Microsoft MVP
..Net Developer
http://www.takempis.com
Neither a follower nor a lender be.
"Steve C. Orr, MCSD" <St***@Orr.net> wrote in message
news:%2***************@TK2MSFTNGP10.phx.gbl...
You can intercept the client side enter keypress event of the text box and
then click the correct button using javascript code.
Here's a good example:
http://www.kamp-hansen.dk/pages/show...d=21&menuid=18
You could also try using this free control.
http://www.metabuilders.com/tools/DefaultButtons.aspx
And here's a good article on the subject:
http://www.allasp.net/enterkey.aspx
--
I hope this helps,
Steve C. Orr, MCSD
http://Steve.Orr.net
Developer for Hire
"Philip Townsend" <pt*******@v1tech.com> wrote in message
news:OS*************@tk2msftngp13.phx.gbl... I have an aspx page that contains 2 user controls, each containing a
seperate textbox and button. I would like to specify that one of the
buttons recieve focus when the page loads. Also, I would like that same
button to fire its event when the user presses enter. Can anybody help
with this?
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!