asp.net how do you find the TextBox that had focus when the post happened

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ELohman
    New Member
    • Dec 2008
    • 1

    asp.net how do you find the TextBox that had focus when the post happened

    Is there a way to know which control had focus when the post in the browser happened ?

    Thank you in advance.
  • Frinavale
    Recognized Expert Expert
    • Oct 2006
    • 9749

    #2
    You'll have to write some JavaScript that keeps track of which field has focus by storing the value in a HiddenField. This HiddenField can then be accessed by the server and you'll know which TextBox had focus last.

    Use the TextBox's JavaScript onfocus event to execute the the JavaScript that stores it's client ID in the HiddenField.

    Comment

    • r035198x
      MVP
      • Sep 2006
      • 13225

      #3
      I wonder why you'd need that. Most of the time it will be the control that triggered the submit. e.g If a text field has focus, when the submit button is clicked, the submit button will be the one given focus before submitting.

      Comment

      • balabaster
        Recognized Expert Contributor
        • Mar 2007
        • 798

        #4
        Originally posted by r035198x
        I wonder why you'd need that. Most of the time it will be the control that triggered the submit. e.g If a text field has focus, when the submit button is clicked, the submit button will be the one given focus before submitting.
        Doesn't account for AutoPostback. I'm in agreement with Frinny's suggestion:

        Write a function that updates a hidden field and add handlers to the OnFocus and OnBlur events to call that function attaching the id of the currently focused object. If you want to know the last focussed object rather than the currently focussed object, then omit the OnBlur event handler.

        In the postback check the value of the hidden field.

        Comment

        Working...