AutoPostback with TextBox loses focus

Little JB picture Little JB · Oct 6, 2008 · Viewed 21.6k times · Source

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?

Answer

Atanas Korchev picture Atanas Korchev · Oct 6, 2008

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.