Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Maintain Panel scroll position in Ajax Update Panel

0.00/5 (No votes)
18 Sep 2011 1  
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

This articles was originally at wiki.asp.net but has now been given a new home on CodeProject. Editing rights for this article has been set at Bronze or above, so please go in and edit and update this article to keep it fresh and relevant.

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

    }


 

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here