ASP.NET AJAX UpdatePanel lost focus problem

John Avis by | September 30, 2013 | ASP.NET Web Forms

Solution to the ASP.NET UpdatePanel lost focus on post back issue.
If you like to use a keyboard to navigate a web form that uses an ASP.NET UpdatePanel, you will soon find that once a post back occurs you lose your position in the form.

There has been several solutions to this problem, but I finally found one that works really well. Unlike many, it handles all possibilities: (a) you tab to the next field; (b) you shift-tab to the previous field; and, (c) you stay in the same field (eg. use arrow keys to select DropDownList option).


First step is to create a JavaScript file and add the following code:
var lastFocusedControlId = "";

function focusHandler(e) {
document.activeElement = e.originalTarget;

function appInit() {
if (typeof (window.addEventListener) !== "undefined") {
window.addEventListener("focus", focusHandler, true);

function pageLoadingHandler(sender, args) {
lastFocusedControlId = typeof (document.activeElement) === "undefined"
? "" :;

function focusControl(targetControl) {
if (Sys.Browser.agent === Sys.Browser.InternetExplorer) {
var focusTarget = targetControl;
if (focusTarget && (typeof (focusTarget.contentEditable) !== "undefined")) {
oldContentEditableSetting = focusTarget.contentEditable;
focusTarget.contentEditable = false;
else {
focusTarget = null;
if (focusTarget) {
focusTarget.contentEditable = oldContentEditableSetting;
else {

function pageLoadedHandler(sender, args) {
if (typeof (lastFocusedControlId) !== "undefined" && lastFocusedControlId != "") {
var newFocused = $get(lastFocusedControlId);
if (newFocused) {


Then in your markup, add a script reference to the JavaScript file you created. In this case I called it Refocus.js and located it in the root folder.
<asp:ScriptManager ID="ScriptManager1" runat="server">
<asp:ScriptReference Path="~/ReFocus.js" />

Related Posts

ASP.NET Web Forms Classic ASP

Custom error pages for ASP.NET Web Forms and Classic ASP in IIS 7 and 8.5

by John Avis | February 20, 2019

I was recently doing some work on a website which has a mixture of older Classic ASP pages and ASP.NET Web Forms pages and ran into problems with custom error pages.

ASP.NET Web Forms

Nicer localised UpdateProgress’ for your ASP.NET pages with multiple UpdatePanels

by John Avis | February 9, 2018

On an ASP.NET Web Forms page with multiple UpdatePanels it’s nice to give the user some feedback when one of the panels is being updated.

Bootstrap ASP.NET Web Forms

ASP.NET Web Forms and Bootstrap 3 and 4 Modals

by John Avis | July 6, 2017

There are a few methods for hiding and showing Bootstrap 3.x and 4.x modals in an ASP.NET Web Forms application. This technique is pure ASP.NET (requires no client script) and allows you to show and hide modals by changing the Visible property of a containing PlaceHolder or Panel.


There are no comments yet. Be the first to leave a comment!

Leave a Comment
ASP.NET Html Forms ASP.NET MVC ASP.NET Web Forms ASP.NET Web Pages Bootstrap C# Classic ASP Cool Websites Databases eBay and PayPal Electrical Repairs General Hardware HTML/CSS Jquery/Javascript Media Center Mobile Phones Responsive Web Design SEO and Social Networking Web Design Web Development Web Security web+db Website Hosting Windows XP Youtube

About me

...mostly about web development and programming, with a little bit of anything else related to the Internet, computers and technology.


Get the latest posts delivered to your inbox.