ASP.NET MVC3 Razor - Maintain scroll position on p

2019-06-17 03:11发布

问题:

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.