473,406 Members | 2,273 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

button focus and pressing enter

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
3 4190
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Matt | last post by:
<input type="button" onClick="doSomething()"> When the user click HTML button, it will launch doSomething(). But I want the user enter ENTER key, it will have same effect. Please advise....
17
by: Neil Ginsberg | last post by:
OK, this is a stupid thing, but I can't seem to get this to work. I have a form with a subform (in continuous form view). A combo box on the main form has code in the AfterUpdate event which adds a...
2
by: Bratislav Jevtic | last post by:
hi, how to make some push button default on the form? for instance, I've got 2 buttons (Submit, Cancel). submit is on the left, cancel on the right side. on enter key pressed, cancel button is...
2
by: Ben Fidge | last post by:
Hi I'm writing a web application where the entire site is driven through a single page, and content is dynamically loaded using Web UserControls. This is achieved by using Page.LoadControl()...
12
by: SJ | last post by:
Hope someone can help me out there I'm struggling with a particular problem... I have a form with many tab pages. On one tab page I've got a button which when clicked with a mouse adds items...
21
by: Darin | last post by:
I have a form w/ a textbox and Cancel button on it. I have a routine to handle textbox.validating, and I have the form setup so the Cancel button is the Cancel button. WHen the user clicks on...
3
by: Patrick [MSFT] | last post by:
Let me preface this with the goal I'm trying to achieve is mimic a feature of another language (Dexterity used by Microsoft Dynamics) and so while a filling a drop down list is a workable solution...
1
by: Claire | last post by:
I'm writing an application with custom skinned controls. I have a text editor form with a rich edit control. Because there's no toolbuttons in the skinned control set, I have to use normal ones for...
24
by: MichaelK | last post by:
Who knows how to prevent submitting a form on the press Enter button before all fields on the form are filled up. People just enter the first field hit Enter and it submits the form and doing...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.