views:

2660

answers:

2

A textbox is set to AutoPostback as changing the value should cause a number of (display-only) fields to be recalculated and displayed. That works fine. However, when the field is tabbed out of, the focus briefly moves on to the next field, then disappears when the page is redrawn so there is no focus anywhere.

I want the focus to be on the new field, not the textbox I've just changed. Is there a way to work out which field had the focus and force it to have it again when the page is redrawn?

+2  A: 

This is "by design". If you are using ASP.NET 2.0+ you can try calling the Focus method of your TextBox once the postback occurs (preferably in the TextChanged event of the TextBox).

I am not sure if there is any built-in way to track focus but I found this article in CodeProject which should do the trick.

korchev
The problem is that I need to set the focus to the control it moved to. Is there a way of identifying it?
Little JB
Thanks for your answer, korchev, but it's not quite what I want. I've clarified my question.
Little JB
We'd tried a few that didn't work. Yours did. Thanks, korchev.
Little JB
+1  A: 

You could also consider refresh display-only fields using AJAX UpdatePanel. This way you won't lose focus from the new field.

Also I have proposed pure server-side solution based on WebControl.Controls.TabIndex analysis, you can use it, if you like.

Alexander Prokofyev