By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,367 Members | 1,281 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,367 IT Pros & Developers. It's quick & easy.

button focus and pressing enter

P: n/a
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!
Nov 17 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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!

Nov 17 '05 #2

P: n/a
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!


Nov 17 '05 #3

P: n/a
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

"Kevin Spencer" <ke***@takempis.com> wrote in message
news:ep**************@TK2MSFTNGP10.phx.gbl...
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!



Nov 17 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.