Detect When a Control Gains Focus

Discussion in 'ASP .Net' started by StinkyDuck, Nov 15, 2007.

  1. StinkyDuck

    StinkyDuck Guest

    Is there a way to detect when a control gains focus on a webform? Say I
    have two text boxes. After the user types in information on the first text
    box and hits the tab key or clicks on the second text box, I want to
    automatically fill in the second text box with the information from the
    first text box.

    I just can't seem to find an event handler for when the control gains/loses
    focus.

    -StinkyDuck
    StinkyDuck, Nov 15, 2007
    #1
    1. Advertising

  2. StinkyDuck,
    "focus" is a client-side, in - the - browser event that can be handled with
    Javascript. Usually the event name is onfocus. if you are looking to fill in
    a second textbox when the user leaves the first, onblur would probably be
    more useful.

    --Peter
    "Inside every large program, there is a small program trying to get out."
    http://www.eggheadcafe.com
    http://petesbloggerama.blogspot.com
    http://www.blogmetafinder.com



    "StinkyDuck" wrote:

    > Is there a way to detect when a control gains focus on a webform? Say I
    > have two text boxes. After the user types in information on the first text
    > box and hits the tab key or clicks on the second text box, I want to
    > automatically fill in the second text box with the information from the
    > first text box.
    >
    > I just can't seem to find an event handler for when the control gains/loses
    > focus.
    >
    > -StinkyDuck
    >
    >
    >
    =?Utf-8?B?UGV0ZXIgQnJvbWJlcmcgW0MjIE1WUF0=?=, Nov 15, 2007
    #2
    1. Advertising

  3. You can do it on client side with javascript. Handle onfocus event:

    myTextBox2.Attributes["onfocus"] = "myHandler()"

    and myHandler will be a javascript function with something like
    Form1.myTextBox2.value = Form1.myTextBox1.value;

    You may have to refer to the textboxes in some other way depending on where
    they are located.

    --
    Eliyahu Goldin,
    Software Developer
    Microsoft MVP [ASP.NET]
    http://msmvps.com/blogs/egoldin


    "StinkyDuck" <> wrote in message
    news:...
    > Is there a way to detect when a control gains focus on a webform? Say I
    > have two text boxes. After the user types in information on the first

    text
    > box and hits the tab key or clicks on the second text box, I want to
    > automatically fill in the second text box with the information from the
    > first text box.
    >
    > I just can't seem to find an event handler for when the control

    gains/loses
    > focus.
    >
    > -StinkyDuck
    >
    >
    Eliyahu Goldin, Nov 15, 2007
    #3
  4. As others have mentioned, JavaScript (onblur/onfocus) is the preferred way.

    However, you could set AutoPostBack=true and create a OnChange handler to do
    what you want on the server. This approach is riddled with problems and not
    recommended, but it is available nonetheless.


    "StinkyDuck" <> wrote in message
    news:...
    > Is there a way to detect when a control gains focus on a webform? Say I
    > have two text boxes. After the user types in information on the first
    > text box and hits the tab key or clicks on the second text box, I want to
    > automatically fill in the second text box with the information from the
    > first text box.
    >
    > I just can't seem to find an event handler for when the control
    > gains/loses focus.
    >
    > -StinkyDuck
    >
    Scott Roberts, Nov 15, 2007
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. MikL
    Replies:
    4
    Views:
    810
    Andrei Kouznetsov
    Nov 13, 2004
  2. Jason
    Replies:
    4
    Views:
    1,618
    Jason
    May 7, 2007
  3. David A. Black
    Replies:
    6
    Views:
    119
    James Britt
    Aug 4, 2004
  4. Roger
    Replies:
    3
    Views:
    322
  5. Ramon F Herrera

    Dramatic performance gains with Libxml

    Ramon F Herrera, Sep 16, 2012, in forum: XML
    Replies:
    2
    Views:
    1,091
    Joe Kesselman
    Sep 20, 2012
Loading...

Share This Page