[分享]如何保证页面刷新后的滚动条位置


http://expert.csdn.net/Expert/topic/2991/2991206.xml?temp=.9256098
Keeping Scroll Position over post Backs:
http://aspalliance.com/articleViewer.aspx?aId=356&pId=1
http://dotnet.aspx.cc/ShowDetail.aspx?id=0C25068E-55EA-4124-8CD6-726ECF5D5D3B
http://expert.csdn.net/Expert/topic/2747/2747150.xml?temp=.1869013


<%@ Page SmartNavigation=ture %>

SmartNavigation=ture
加上这个好象整个页面都变的乱七八糟的,原来引用的CSS文件都失效了,字体乱成一团,TABALE里面的组件也东一个西一个


//使用cookie ,js:
<script id=clientEventHandlersJS language=javascript>
<!--

window.onload =function() {
if(getcookie("height")!=null)
document.body.scrollTop=getcookie("height");
}

window.onunload =function () {
    setcookie("height",document.body.scrollTop);
}
function getcookie(sName)
{
  var aCookie = document.cookie.split("; ");
  for (var i=0; i < aCookie.length; i++)
  {
    var aCrumb = aCookie[i].split("=");
    if (sName == aCrumb[0])
      return unescape(aCrumb[1]);
  }
  return null;
}

function setcookie(sName, sValue)
{
  date = new Date();
  date.setTime(date.getTime()+60000);  //1 minute
  document.cookie = sName + "=" + escape(sValue) + "; expires=" + date.toGMTString();
}

//-->
</script>

设置SmartNavigation=ture(页面或者config中)会有异常情况,以下方法可以解决该问题
环境为VS.NET 2000和VS.NET 2003通过

<script language="javascript">
function GetCookie (name)
{
   var arg = name + "=";
   var alen = arg.length;
   var clen = document.cookie.length;
   var i = 0;
   while (i < clen)
   {
       var j = i + alen;
       if (document.cookie.substring(i, j) == arg)
       return getCookieVal (j);
       i = document.cookie.indexOf(" ", i) + 1;
       if (i == 0) break;
   }
 return null;
}

function getCookieVal (offset)
{
   var endstr = document.cookie.indexOf (";", offset);
   if (endstr == -1)
     endstr = document.cookie.length;
     return unescape(document.cookie.substring(offset, endstr));
   }
function SetCookie (name, value)
   {
     document.cookie = name + "=" + escape (value)
   }
</script>

在body中加入
<body bgColor="#f1f5f5" leftMargin="0" topMargin="0" rightMargin="0" MS_POSITIONING="GridLayout" onload="document.body.scrollTop=GetCookie('posy')" onunload="SetCookie('posy',document.body.scrollTop)">

posted @ 2005-09-13 13:25  PointNet  阅读(1141)  评论(0编辑  收藏  举报