Mast3r
Mast3r

Reputation: 73

ASP.NET MVC3 Razor - Maintain scroll position on postback

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

Upvotes: 7

Views: 11600

Answers (3)

Anupam Singh
Anupam Singh

Reputation: 1166

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.

Upvotes: 0

takepara
takepara

Reputation: 10433

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.

Upvotes: 0

Erik Funkenbusch
Erik Funkenbusch

Reputation: 93444

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.

Upvotes: 1

Related Questions