Saturday, January 29, 2011

Maintain Scroll position in div Asp.Net

This javascript is useful for maintaining scroll position in Panel or div tag...
Write following javascript function inside js file and add script reference to this file on page Or write following javascript on the same page inside script tag, it is called as inline javascript.

Following code in Script Tag....
window.onload = function(){
var strCook = document.cookie;
if(strCook.indexOf("!~")!=0){
var intS = strCook.indexOf("!~");
var intE = strCook.indexOf("~!");
var strPos = strCook.substring(intS+2,intE);
document.getElementById("divTest").scrollTop = strPos;
}
}
function SetDivPosition(){
var intY = document.getElementById("divTest").scrollTop;
document.title = intY;
document.cookie = "yPos=!~" + intY + "~!";
}


Following code in body

You need to call SetDivPosition() javascript function on div's onscroll event
< div id="divTest" width:150px;height:200px;overflow:auto" onscroll="SetDivPosition()">
--Gridview (or more data)
</div>
This way you can maintain the scroll position.

Happy Coding!!

No comments:

Post a Comment