轻松解决页面回传后页面滚动到顶端
页面回传后页面滚动到顶端,造成这一问题的原因是页面回传,其实等于载入一次页面,新载入的页面会定点到页面的顶端。这一特点会造成很差的用户使用感受,如何解决这一问题呢,我们可以借用隐藏的viewstate机制保存当前页面滚动的位置,在页面回传后调用。
<%@ page language="c#" codebehind="test_thin.aspx.cs" autoeventwireup="false" inherits="eim.web.test_thin" %>
<!doctype html public "-//w3c//dtd html 4.0 transitional//en" >
<html>
<head>
<title>test_thin</title>
<meta name="generator" content="microsoft visual studio 7.0">
<meta name="code_language" content="c#">
<meta name="vs_defaultclientscript" content="javascript">
<meta name="vs_targetschema" content="http://schemas.microsoft.com/intellisense/ie5">
<script language="javascript">
function savescroll()
{
var hid = document.getelementbyid("hid");
hid.value = document.body.scrolltop;
}
function getscroll()
{
var hid = document.getelementbyid("hid");
//hid.value = document.body.scrolltop;
document.body.scrolltop = hid.value;
}
</script>
</head>
<body ms_positioning="gridlayout" onload="getscroll()" onscroll="savescroll()">
<form id="test_thin" method="post" runat="server" onsubmit="savescroll()">
<input style="z-index: 101; left: 216px; position: absolute; top: 127px" type="hidden" id="hid" runat="server">
<asp:button id="button1" style="z-index: 102; left: 37px; position: absolute; top: 1529px" runat="server" text="button"></asp:button>
</form>
</body>
</html>