button focus and pressing enter

  • Thread starter Thread starter Philip Townsend
  • Start date Start date
P

Philip Townsend

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?
 
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_RegularSearch_btnSearchSubmit').click
();
break;
case "AirportSearch_DistanceSearch_txtAirportCity":
case "AirportSearch_DistanceSearch_txtAirportDistance":

document.getElementById('AirportSearch_DistanceSearch_btnSearchSubmit').clic
k();
break;
case "AirportSearch_AdvancedSearch_txtAirportID":
case "AirportSearch_AdvancedSearch_txtAirportName":
case "AirportSearch_AdvancedSearch_txtAirportCity":

document.getElementById('AirportSearch_AdvancedSearch_btnSearchSubmit').clic
k();
break;
case "HeaderLogin1_txtPassword":
document.getElementById('HeaderLogin1_ImageButtonLoginGo').click();
break;
case "HeaderLogin1_txtAirportID":
document.getElementById('HeaderLogin1_ImageButtonAirportGo').click();
break;
case "Login1_txtUserPassword":
document.getElementById('Login1_btnLoginSubmit').click();
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.
 
Private Sub SetInputFocus(ByVal ctlName As String)
Dim sb As String

sb = "<script language=javascript>"
sb = sb + "function setNewFocus(ctl)"
sb = sb + "{"
sb = sb + "if (document.forms[0][ctl] != null)"
sb = sb + "{"
sb = sb + "document.forms[0][ctl].focus();"
sb = sb + "}"
sb = sb + "var range = document.forms[0][ctl].createTextRange();"
sb = sb + "range.move('textedit');"
sb = sb + "range.select();"
sb = sb + "}"
sb = sb + "setNewFocus('"
sb = sb + ctlName
sb = sb + "');"
sb = sb + "</script>"
If Not IsStartupScriptRegistered("InputFocusHandler") Then
RegisterStartupScript("InputFocusHandler", sb.ToString())
Session("strInputFocus") = ctlName
End If
End Sub


Enjoy!

-Big T
 
Back
Top