Professional Web Applications Themes

button focus and pressing enter - ASP.NET General

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!...

  1. #1

    Default 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!
    Philip Guest

  2. #2

    Default Re: button focus and pressing enter

    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/showdoc.asp?id=28&menuid=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" <com> wrote in message
    news:phx.gbl... 


    Steve Guest

  3. #3

    Default Re: button focus and pressing enter

    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;
    }
    doent.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_Regularch_txtAirportID":
    case "AirportSearch_Regularch_txtAirportName":
    case "AirportSearch_Regularch_txtAirportCity":

    doent.getElementById('AirportSearch_Regular ch_btnSearchSubmit').click
    ();
    break;
    case "AirportSearch_DistanceSearch_txtAirportCity":
    case "AirportSearch_DistanceSearch_txtAirportDistan ce":

    doent.getElementById('AirportSearch_DistanceSea rch_btnSearchSubmit').clic
    k();
    break;
    case "AirportSearch_AdvancedSearch_txtAirportID":
    case "AirportSearch_AdvancedSearch_txtAirportName":
    case "AirportSearch_AdvancedSearch_txtAirportCity":

    doent.getElementById('AirportSearch_AdvancedSea rch_btnSearchSubmit').clic
    k();
    break;
    case "HeaderLogin1_txtPassword":
    doent.getElementById('HeaderLogin1_ImageButtonL oginGo').click();
    break;
    case "HeaderLogin1_txtAirportID":
    doent.getElementById('HeaderLogin1_ImageButtonA irportGo').click();
    break;
    case "Login1_txtUserPassword":
    doent.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" <net> wrote in message
    news:%phx.gbl... 
    >
    >[/ref]


    Kevin Guest

  4. #4

    Default Re: button focus and pressing enter

    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 (doent.forms[0][ctl] != null)"
    sb = sb + "{"
    sb = sb + "doent.forms[0][ctl].focus();"
    sb = sb + "}"
    sb = sb + "var range = doent.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" <com> wrote in message
    news:phx.gbl... 

    main 
    the 
    which 
    loses 
    doent.getElementById('AirportSearch_Regular ch_btnSearchSubmit').click 
    doent.getElementById('AirportSearch_DistanceSea rch_btnSearchSubmit').clic 
    doent.getElementById('AirportSearch_AdvancedSea rch_btnSearchSubmit').clic [/ref]
    and [/ref][/ref]
    same 
    > >
    > >[/ref]
    >
    >[/ref]


    Todd Guest

  5. #5

    Default button focus and pressing enter

    Can anybody tell me how I can disable the enter (keyboard) button from
    being able to submit a form? In other words, force the user to click a
    button with the mouse?

    *** Sent via Developersdex http://www.developersdex.com ***
    Don't just participate in USENET...get rewarded for it!
    Philip Guest

  6. #6

    Default Re: button focus and pressing enter

    Heres a simple way, not the best way maybe.

    <script language="javascript">
    function keyPress()
    {
    if (event.keyCode == 13)
    {
    event.returnValue = false;
    }
    }
    </script>


    <body onkeypress="KeyPress();">
    or you can specify each textbox, etc.. by placing code like this in the
    Page_Load Event
    this.tbCode.Attributes.Add("onkeypress", "keyPress();");

    the same technique can be used to fire an event like save, or search
    function keyPress()
    {
    if (event.keyCode == 13)
    {
    event.returnValue = false;
    doent.getElementById("btnDoSearch").click();
    }
    }

    Karl



    "Philip Townsend" <com> wrote in message
    news:Os3sx%phx.gbl... 


    karl Guest

  7. #7

    Default Re: button focus and pressing enter

    It seems as if the onkeypress event is not even firing...

    *** Sent via Developersdex http://www.developersdex.com ***
    Don't just participate in USENET...get rewarded for it!
    Philip Guest

Similar Threads

  1. Pressing Enter s up my template - line breaksimages
    By layziebone123 in forum Macromedia Dynamic HTML
    Replies: 6
    Last Post: September 2nd, 12:52 PM
  2. How control which button has focus [ENTER]
    By Olivier Matrot in forum ASP.NET Data Grid Control
    Replies: 1
    Last Post: February 17th, 05:05 PM
  3. Popup; show, take focus, wait for button action, give focus, hide
    By John van Terheijden in forum Macromedia Flash
    Replies: 0
    Last Post: February 2nd, 12:08 PM
  4. input=text fires Submit when pressing Enter
    By Jerry in forum ASP.NET General
    Replies: 4
    Last Post: July 25th, 01:38 PM
  5. Get text in TextBox without pressing Enter key
    By mg in forum ASP.NET General
    Replies: 5
    Last Post: July 14th, 02:09 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139