Ajax Update Panel always resets the scroll position of a panel within it, after postback. Means if you have a panel inside the ajax update panel then you will experience that panel's scroll position is always set soon after the postback.
This behavior becomes more annoying to the user when panel contains entry fields and after an entry postback is required where the scroll position is reset aswell. In this way the user will have to manually scroll down the panel to reach to the next input control for entry.
Many developers face this problem and the solution with which you can reset the scroll position soon after the page is back from postback is here;
Guidelines:
- Have a hidden field lets say 'hfScrollvalue' ( to save and reset scroll position )
- Write a JS method "GetScrollPosition", to get the scroll position of the panel and save in your hidden field
- Write a JS method "SetScrollPosition" to get the scroll position from the hidden field and set it back to the panel
- You can then call the SetScrollPosition method when your request is end by adding a handler for this purpose
- After the request ends the scroll position is set again by update panel. So you can give a bit delay in setting your value to the panel scroll position. You can do so by using setTimeout method of Javascript. so by this delay your saved position will be set on the panel after the ajax processing is done
You can use the following code:
function GetScrollPosition() {
document.getElementById('<%= hfScrollvalue.ClientID %>').value = document.getElementById('<%= Panel1.ClientID %>').scrollTop;
}
function SetScrollPosition() {
document.getElementById('<%= Panel1.ClientID %>').scrollTop = document.getElementById('<%= hfScrollvalue.ClientID %>').value;
}
}
Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequest);
function EndRequest(sender, args) {
setTimeout('SetScrollPosition()', 60);
}
If you are working with a TextBox, that is inside an UpdatePanel, that is also tied to a Timer, then you can to something like this:
Sys.Application.add_init(DownloadInit);
function DownloadInit(sender) {
Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequest);
}
function SetScrollPosition() {
var textArea = document.getElementById('<%=TextBox1.ClientID%>');
textArea.scrollTop = textArea.scrollHeight;
}
function EndRequest(sender, args) {
SetScrollPosition();
}