Click here to Skip to main content
Click here to Skip to main content

Maintain Panel scroll position in Ajax Update Panel

, 11 Oct 2013
Rate this:
Please Sign up or sign in to vote.
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

Editorial Note

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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

ASP.NET Community

United States United States
The ASP.NET Wiki was started by Scott Hanselman in February of 2008. The idea is that folks spend a lot of time trolling the blogs, googlinglive-searching for answers to common "How To" questions. There's piles of fantastic community-created and MSFT-created content out there, but if it's not found by a search engine and the right combination of keywords, it's often lost.
 
The ASP.NET Wiki articles moved to CodeProject in October 2013 and will live on, loved, protected and updated by the community.
Group type: Collaborative Group

247 members


Comments and Discussions

 
Questionissue related on maintain scroll position Pingroupparas_zalariya126-Apr-14 20:34 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web02 | 2.8.140821.2 | Last Updated 11 Oct 2013
Article Copyright 2013 by ASP.NET Community
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid