Maintain scroll position after async postback from update panel

跟風遠走 提交于 2019-12-22 03:58:29

问题


I'm having some troubles with asp.net and update panel. The problem is, that every time partial postback occurs from update panel, page is scrolled back to top. On most of my pages this is not such a big problem, but on some pages can get quite long. Then, when user is on bottom of page, I show jQuery popup with RadListView in it, and user can select element in this list. But clicking on this element causes partial postback and page jumps back to the top.

I've looked through internet and could not find any solution to my problem. Of course setting MaintainScrollPositionOnPostback does nothing.

Does anyone know anything that could help me deal with this problem?

Cheers, Pako


回答1:


There is a little workaround for this, that I've used in an ERP long time ago. Not sure if it's the best solution, but it works.

I don't know if you use a custom Page class or the default System.Web.UI.Page one, but, I'll try to explain to you how you do it, and then you find out the best way you can implement it in your environment, alright?

You'll create a HiddenField, for example, with the ID "hfScrollPosition".

Then, you'll make a javascript event: document.onscroll or something like that, and inside the event you'll update the hidden field to get the current scroll position. For example: document.getElementById("hfScrollPosition").value = document.documentElement.scrollTop;

Doing that, you'll have an ASP.NET control updating its value dinamically, according to the body scroll position. So, when some control in your page makes a postback, you can put the following javascript code in your Page_Load event:

document.documentElement.scrollTop = document.getElementById("hfScrollPosition").value;

So, everytime your page gets a postback, the body scroll position will be correctly updated.


EDIT: I've made a fiddle to simulate it: https://jsfiddle.net/j26fpgzo/




回答2:


Use the ID of some control in the repeater and use JQuery to scroll after postback is completed.

You can get the ID of some control as per the format they are generating.



来源:https://stackoverflow.com/questions/6583501/maintain-scroll-position-after-async-postback-from-update-panel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!