I have a listbox inside an update panel. When I scroll down and select an item, it scrolls back to the top of the listbox. I heard that the dom does not keep track of the scroll position on a postback. Does anyone have a solution/example on how to solve this?
Thanks, XaiSoft
You can still achieve the partial postback using the
UpdatePanel
containing aListBox
, but prevent it from being overwritten by a new instance by setting the following on theUpdatePanel
:This as stated in the MSDN:
The one caveat to this is if you are trying to do any other visual manipulation to the ListBox from the server-side (i.e. adding
CSS
attributes to the items on postbacks), then you will not see them render until another unrelated postback occurs. If all you are doing is making selections than settingChildrenAsTriggers="False"
will work.For the enterprise application I was working on, it turned out that the MasterPage.Master page was handling the Sys.WebForms.PageRequestManager.getInstance().add_endRequest event and setting window.scroll(0,0) within it. The content pages were obviously executing the window.scroll(0,0) on each trigger of the contentpage's UpdatePanel. I tried overriding the add_endRequest functionality in the content page but could not get that to work. Eventually I just modified the Master.Master to not do window.scroll(0,0).
It seems that the following code example scrolls in the following way:
Internet Explorer 8: After postback the selected item is the first visible item.
Firefox: After postback the selected item is always visible (but might be the last visible item).
Chrome: After postback, the selected item might be hidden since the listbox scrolls to the top, as you say.
//Maintain Scroll Position for Panel/Div with out Update panel
You're running into this problem because the
UpdatePanel
completely replaces your scrolled<select>
element with a new one when the asynchronous request comes back.Possible solutions:
Use JavaScript to store the
scrollTop
property of the<select>
element in a hidden form element before theUpdatePanel
is submitted (by calling theClientScriptManager.RegisterOnSubmitStatement
method) and then setting it on the new<select>
when the AJAX call comes back. This will be tedious, error-prone, and probably not very compatible (see here).Use JavaScript to store the
<select>
'sselectedIndex
property and re-select that item when the AJAX call comes back. Obviously this won't work if the user hasn't selected anything yet.Don't use
UpdatePanel
s. Try jQuery + ASP.NET page methods instead.