2010-04-24 3 views
5

के बाद अपडेटपैनल में फ़ोकस स्थिति को कैसे बनाए रखूं, मेरे पास अपडेट पैनल वाले पृष्ठ में चार नियंत्रण हैं। प्रारंभ में माउस फोकस पहले नियंत्रण पर सेट है। जब मैं आंशिक रूप से पेज पर सर्वर को वापस पोस्ट करता हूं तो फोकस स्वचालित रूप से नियंत्रण से पिछले केंद्रित नियंत्रण से पहले नियंत्रण में जाता है जिसे मैंने नीचे दबाया है। आखिरी फोकस बनाए रखने का कोई तरीका है?पृष्ठ आंशिक पोस्ट वापस

उत्तर

10

Restoring Lost Focus in the Update Panel with Auto Post-Back Controls पर एक नज़र डालें:

समाधान के पीछे मूल विचार से पहले अद्यतन पैनल अद्यतन किया जाता है इनपुट ध्यान देने के साथ नियंत्रण की आईडी बचाने के लिए और इनपुट सेट के बाद कि नियंत्रण करने के लिए वापस ध्यान केंद्रित करने की है अद्यतन पैनल अद्यतन किया गया है।

मैं निम्नलिखित जावास्क्रिप्ट के साथ आता हूं जो अद्यतन पैनल में खोए गए फ़ोकस को पुनर्स्थापित करता है।

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

मैं इस और अधिक सुरुचिपूर्ण लगता है:

(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); 
    }); 
})();