Detect When a Control Gains Focus

S

StinkyDuck

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
 
E

Eliyahu Goldin

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

Scott Roberts

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top