2010-04-24 18 views

cevap

10

Restoring Lost Focus in the Update Panel with Auto Post-Back Controls bir göz atın:

çözümü ardındaki temel fikir güncelleme paneli güncellenir önce giriş odaklı denetim kimliği kaydetmek ve sonra o kontrole geri odak girişi ayarlamaktır güncelleme paneli güncellenir.

Kayıp odağı güncelleştirme bölmesinde geri yükleyen şu JavaScript ile geliyorum.

var lastFocusedControlId = ""; 

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

function appInit() { 
    if (typeof(window.addEventListener) !== "undefined") { 
     window.addEventListener("focus", focusHandler, true); 
    } 
    Sys.WebForms.PageRequestManager.getInstance().add_pageLoading(pageLoadingHandler); 
    Sys.WebForms.PageRequestManager.getInstance().add_pageLoaded(pageLoadedHandler); 
} 

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

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; 
     } 
     targetControl.focus(); 
     if (focusTarget) { 
      focusTarget.contentEditable = oldContentEditableSetting; 
     } 
    } 
    else { 
     targetControl.focus(); 
    } 
} 

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

Sys.Application.add_init(appInit); 
3

Bunu daha zarif bulmak:

(function(){ 
    var focusElement; 
    function restoreFocus(){ 
     if(focusElement){ 
      if(focusElement.id){ 
       $('#'+focusElement.id).focus(); 
      } else { 
       $(focusElement).focus(); 
      } 
     } 
    } 

    $(document).ready(function() { 
     $(document).on('focusin', function(objectData){ 
      focusElement = objectData.currentTarget.activeElement; 
     }); 
     Sys.WebForms.PageRequestManager.getInstance().add_endRequest(restoreFocus); 
    }); 
})(); 
İlgili konular