Maintain Scroll Position of DIV using JavaScript – ASP.NET 2.0

Some people have some problems in maintaining the position of the DIV. Initially, The most of people are using htc for solving this problem. but there are so many disadvantages of using htc such as it works only in Internet Explorer, it doesn’t work properly with Visual Studio 2005 and so on.

Finally, I got the solution for solving this problem.. Using Javascript for keeping the scroll position is the best way to go.. JavaScript helps us to know the current position of DIV. Plus, we can use HTTP Cookie for storing the value of the previous position of DIV tag.

SouceCode Download :

window.onload = function(){
var strCook = document.cookie;
var intS = strCook.indexOf("!~");
var intE = strCook.indexOf("~!");
var strPos = strCook.substring(intS+2,intE);
document.getElementById("grdWithScroll").scrollTop = strPos;
function SetDivPosition(){
var intY = document.getElementById("grdWithScroll").scrollTop;
document.title = intY;
document.cookie = "yPos=!~" + intY + "~!";

I have tested this code with IE6, 7 and Mozilla Firefox. It’s working very fine.
Please let me know if you have any problem with this coding. Thank you.
Special Thanks to Jim Ross and Eric Pascarello.

UPDATED Maintain Scroll Position of DIV using ASP.NET Ajax by Brad Alcock (Thanks for sending this mail, Brad.)

hi. here’s the code with a very brief explanation on what goes on behind the scenes.

a little different to what you did, but sort of on the same track. I’m sure you can find a method of working around it to fit in with your post ^^

right, my div’s id was panel1, I basically created a cookie for the onscroll of the div in the function SetDivPosition(). the read cookie is read using the stringbuilder in the C# Page_load code underneath.

function SetDivPosition(){
var intY = document.getElementById("Panel1").scrollTop;
var date = new Date();
var expires = "; expires="+date.toGMTString();
document.cookie = "cookie1"+"="+intY+expires+"; path=/";
document.title = intY;
function readCookie(name) {
var nameEQ = name + "=";
var ca = document.cookie.split(';');
for(var i=0;i < ca.length;i++) {
var c = ca&#91;i&#93;;
while (c.charAt(0)==' ') c = c.substring(1,c.length);
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
return null;

here is the string builder in the page Load. the part in the if(!IsPostback) is there so that when the page is loaded for the very first time, it removes any possibility of a cookie being there (incase the page is returned to within an hour. ) that means the scrollbar will not suddenly jump to a position at arb times like first time page is loaded.
don't forget to use  System.Text; otherwise it will throw an error.

&#91;sourcecode langague="csharp"&#93; 
protected void Page_Load(object sender, EventArgs e)
if (!IsPostBack)
StringBuilder sbScript = new StringBuilder();
sbScript.Append("<script language='JavaScript' type='text/javascript'>");
sbScript.Append("document.cookie = cookie1 + \"=\" + \"\" + -1 + \"; path=/\";");

// Make use ScriptManager to register the script
ScriptManager.RegisterStartupScript(this, this.GetType(), "@@@@MyCallBackAlertScript", sbScript.ToString(), false);

StringBuilder sbScript1 = new StringBuilder();
sbScript1.Append("<script language='JavaScript' type='text/javascript'>");
sbScript1.Append("var strCook = readCookie(\"cookie1\"); "+
"document.getElementById(\"Panel1\").scrollTop = strCook;");

// Make use ScriptManager to register the script
ScriptManager.RegisterStartupScript(this, this.GetType(), "@@@@MyCallBackAlertScript", sbScript1.ToString(), false);


* Maintain Scroll Position in any Page Element
* Remember a Div’s Scroll Position


Useful Links

Forums N Portal

Riverside Internet Forums (***)

Discuss .NET (****)

Discussion Forum ( A-Board v1.1)

Simple Discussion Forum

Simple Message Board

Updated ASP .NET Forum

Simple open source forum engine

Neat Forum

ITCN ASP.NET Forum 2004

Personal .NET Portal


htmlArea – Turn any TEXTAREA into a WYSIWYG editor 

JavaScript Control Text Design Templates for ASP.NET

Chat Bot

Artificial Intelligence (AI) Chatbot

A Simple ] AI Chatbot Wizard: Create, Train and Chat

Photo Gallery

Web Image Browser – Lots of images and no time to publish them?

“Filmstrip” or CatalogView Photo Gallery Sample (***************)

Photo album browsing control

ASP.NET PhotoBook

Dynamic Photogallery in ASP.NET and C# – V1

An ASP.NET thumbnail solution


Guestbook for ASP.NET

ASP.NET Guestbook Application


Fast ASP.NET Hit Counter with Full Digit Graphic File Support

PleaseWaitButton ASP.NET Server Control

PleaseWaitButton ASP.NET Server Control

Handy XP Style Menu

Yahoo! mail and Hotmail like Rich Text Editor

Another XP style panel bar for Web

Windows XP Style Menu

An Open File Dialog in JavaScript


Yahoo UI (*********)

Nifty Corners

This 3D Gogofrog Web site is worth taking a look at. 
See it at

The Ultimate JavaScript Client Sniffer, Version 3.03: Determining Browser Vendor, Version, and Operating System With JavaScript