I have code to capture the control with the current focus on pageLoading and set the focus back to that control on pageLoaded, but it only ever works the first time, not the second. For example, I have two textboxes that both act as Asynch Postback triggers.
If I change data in one, and tab out, the AJAX performs properly, and the pageLoaded event sets the focus to the control I just tabbed to. If I change data in the next textbox and tab out, the AJAX performs properlly and the pageLoaded event fires (F12 breakpoints
show it does) but the focus is then gone. It does not matter which of the two textboxes I change first, the second one always fails. Even if I have a single textbox that fires an AJAX postback, the first time I tab out, the focus is set properly, if I go
back to that same textbox, change the data and tab out again, it does not.
Any help would be greatly appreciated!
Here is the Javascript that is set as a ScriptReference in the ScriptManager:
/*
Script adapted from
http://couldbedone.blogspot.com/2007/08/restoring-lost-focus-in-update-panel.html
*/
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;
}
prepareFocusedControl(targetControl);
if (focusTarget)
{
focusTarget.contentEditable = oldContentEditableSetting;
}
}
else
{
prepareFocusedControl(targetControl);
}
}
function pageLoadedHandler(sender, args) {
if (typeof(lastFocusedControlId) !== "undefined" && lastFocusedControlId != "") {
var newFocused = $get(lastFocusedControlId);
if (newFocused) {
focusControl(newFocused);
}
}
}
function prepareFocusedControl(targetControl)
{
try
{
targetControl.focus();
}
catch(err)
{
}
}
var keys = new Array();
function keyCapture() {
var key = event.keyCode;
keys[keys.length] = key;
}
Sys.Application.add_init(appInit);
Thank you Chetan, however I am essentially doing the same thing as this post mentions, and as I described above, it works the first time, but it does not work any time after the first. I have put alerts in my endrequest event handler and I see them triggered
every time, but unless it's the very first time, the focus is still completely gone after the alert displays (or even if there is no alert).
To clarify, I am not asking HOW to reset the focus, I'm asking how to get it to work every time, not just the first time.
VG_mnet
Member
22 Points
17 Posts
PageRequestManager pageLoaded event only works the first time?
Nov 06, 2012 07:33 PM|LINK
I have code to capture the control with the current focus on pageLoading and set the focus back to that control on pageLoaded, but it only ever works the first time, not the second. For example, I have two textboxes that both act as Asynch Postback triggers. If I change data in one, and tab out, the AJAX performs properly, and the pageLoaded event sets the focus to the control I just tabbed to. If I change data in the next textbox and tab out, the AJAX performs properlly and the pageLoaded event fires (F12 breakpoints show it does) but the focus is then gone. It does not matter which of the two textboxes I change first, the second one always fails. Even if I have a single textbox that fires an AJAX postback, the first time I tab out, the focus is set properly, if I go back to that same textbox, change the data and tab out again, it does not.
Any help would be greatly appreciated!
Here is the Javascript that is set as a ScriptReference in the ScriptManager:
/* Script adapted from http://couldbedone.blogspot.com/2007/08/restoring-lost-focus-in-update-panel.html */ 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; } prepareFocusedControl(targetControl); if (focusTarget) { focusTarget.contentEditable = oldContentEditableSetting; } } else { prepareFocusedControl(targetControl); } } function pageLoadedHandler(sender, args) { if (typeof(lastFocusedControlId) !== "undefined" && lastFocusedControlId != "") { var newFocused = $get(lastFocusedControlId); if (newFocused) { focusControl(newFocused); } } } function prepareFocusedControl(targetControl) { try { targetControl.focus(); } catch(err) { } } var keys = new Array(); function keyCapture() { var key = event.keyCode; keys[keys.length] = key; } Sys.Application.add_init(appInit);pageloaded
chetan.sarod...
All-Star
66589 Points
11270 Posts
Re: PageRequestManager pageLoaded event only works the first time?
Nov 09, 2012 02:57 AM|LINK
Refer this
http://vincexu.blogspot.in/2009/06/keep-textbox-focus-inside-updatepanel.html
pageloaded
Senior Software Engineer,
Approva Systems Pvt Ltd, Pune, India.
VG_mnet
Member
22 Points
17 Posts
Re: PageRequestManager pageLoaded event only works the first time?
Nov 09, 2012 01:00 PM|LINK
Thank you Chetan, however I am essentially doing the same thing as this post mentions, and as I described above, it works the first time, but it does not work any time after the first. I have put alerts in my endrequest event handler and I see them triggered every time, but unless it's the very first time, the focus is still completely gone after the alert displays (or even if there is no alert).
To clarify, I am not asking HOW to reset the focus, I'm asking how to get it to work every time, not just the first time.
Thanks
-VG
pageloaded
VG_mnet
Member
22 Points
17 Posts
Re: PageRequestManager pageLoaded event only works the first time?
Nov 27, 2012 01:46 PM|LINK
Is there anybody else that can provide some guidance on this question please?
-VG
VG_mnet
Member
22 Points
17 Posts
Re: PageRequestManager pageLoaded event only works the first time?
Jan 14, 2013 03:44 PM|LINK
I am still looking for an answer to this. Can anybody help???
-VG