最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Maintain scroll position after async postback from update panel - Stack Overflow

programmeradmin0浏览0评论

I'm having some troubles with asp 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

I'm having some troubles with asp 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

Share Improve this question edited Jul 5, 2011 at 13:39 Muhammad Akhtar 52.2k37 gold badges139 silver badges191 bronze badges asked Jul 5, 2011 at 13:34 JarekJarek 3,3791 gold badge29 silver badges33 bronze badges 2
  • 1 Duplicate: stackoverflow./questions/5288682/… – Despertar Commented May 19, 2012 at 23:25
  • …or of Reset scroll position after Async postback - ASP.NET – Bergi Commented Apr 4, 2013 at 13:38
Add a ment  | 

2 Answers 2

Reset to default 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/j26fpgzo/

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

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

发布评论

评论列表(0)

  1. 暂无评论