Subscribe to Youtube channel

Restore the lost focus of Auto post back controls in update Panel control

We put  autopostback controls like textbox, DropDownList controls inside in update panel to reducing the flickering of the Page.but when user uses tab for entering the details  the focus of the control losts. Web site users who prefer to use keyboard need to use mouse to activate appropriate input box or press TAB multiple times.


Step 1:

Save following in js file give any name to it (like MyUpdatePanelFocus.js)

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) {


Step 2: Do the following setting in aspx page

    <asp:ScriptManager ID="c_scriptManager" runat="server">
            <asp:ScriptReference Path="~/MyUpdatePanelFocus.js" />

That's it you have done it!!!!


  1. Thank you soooo much this is the only script i have found that has worked the way i wanted it to and was easy to implement.

  2. GOOd work friend......keep it up.i am seraching thgis type code

  3. Nice work, worked perfectly.

  4. Hi,
    But it's not working in ModlelPopupExtender.
    u people having any solution.

  5. oh nice yaar it is working ......thanks dear

  6. Great Work.. After a long search got a perfect solution.

  7. Really good concept (the most important pat). Great would be to also keep the cursor position for cases when user is in middle of editing.

  8. realy great work!

  9. This comment has been removed by the author.

  10. I have tried this but the issue is that the position of the cursor is not being set to where the user clicked after editing in the previous text area. but if there is no edit to the previous control, the cursor position is maintained and is set to the user's desired location. Could anyone please help on this


Post a Comment

Popular posts from this blog

Root your Gingerbread 2.3.4 android Mobile Phone

Implement Logging in CSV file using Nlog in .net core MVC application- part 2

Disable backspace key using Jquery

Devexpress Datebox date formatting in angular 6 with example

Implement Nlog in .Net core MVC application part 1

How to Import CSV File and bind csv file data to gridview in using c#

Angular User Session Timeout example step by step

Clone a generic list in C# using extension method