ASP.NET MVC3 Razor - Maintain scroll position on postback

徘徊边缘 提交于 2019-12-23 08:11:41

问题


How can i maintain scroll position on postback after sorting a grid table that uses MvcContrib framework?


回答1:


The usual way is to use some javascript to set the current scroll position to a hidden field, then restore that position on page load (usually in a jquery ready event).

However, that's really just a side effect. You should be doing some kind of ajax command to update the grid rather than a postback, then no scrolling required.




回答2:


Use jQuery and client side cookie.

$(function(){
  var posName = location.href + "_top";
  $(window).unload(function() {
    var top = $(document).scrollTop();
    $.cookie(posName, top);
  });

  var goTop = parseInt($.cookie(posName));
  if (goTop) {
    $(document).scrollTop(goTop);
    $.cookie(posName, "");
  }
});

Hope this code.




回答3:


A useful solution is posted here : http://www.experts-exchange.com/Hardware/Servers/Q_28082177.html

$(function(){

        var top = parseInt($.cookie("top"));
        if(top) $(document).scrollTop(top);
        $(document).scroll(function() {
            var top = $(document).scrollTop();
            $.cookie("top", top);
        })
    });

This is a very old thread but I have posted this for developer who will be searching for this kind of issue, may help.



来源:https://stackoverflow.com/questions/7278295/asp-net-mvc3-razor-maintain-scroll-position-on-postback

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