开发者

ASP.NET MVC3 Razor - Maintain scroll position on postback

开发者 https://www.devze.com 2023-04-02 02:22 出处:网络
How can i maintain scroll position on开发者_高级运维 postback after sorting a grid table that uses MvcContrib framework?The usual way is to use some javascript to set the current scroll position to a

How can i maintain scroll position on开发者_高级运维 postback after sorting a grid table that uses MvcContrib framework?


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.


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消