How can i maintain scroll position on postback after sorting a grid table that uses MvcContrib framework?
Asked
Active
Viewed 1.1k times
3 Answers
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.

Erik Funkenbusch
- 92,674
- 28
- 195
- 291
-
1This solution is great but i was wondering if i could find something better and easier, like the property MaintainScrollPositionOnPostback=true in ASP.NET Web Forms. I'm using the MVC Contrib grid with search filters and sorting so i expected a built-in solution but i can't find it... – Mast3r Sep 06 '11 at 10:58
-
1Yea I agree with Mast3r. Nothing says that we "must" use Ajax. I find using viewmodels in mvc 4 with postback much much faster to develop and easier to maintain. If browsers caching is enabled you don't even see the page flick! solution at http://stackoverflow.com/a/2618726/706363 – Piotr Kula Jun 11 '13 at 15:29
0
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.

Anupam Singh
- 1,158
- 13
- 25
0
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.

takepara
- 10,413
- 3
- 34
- 31
-
That would mean the browser would scroll even if you moved to a different page. I think it's better to use a hidden input, since that will stay on the current page only. – Erik Funkenbusch Sep 02 '11 at 05:34