定位网页滚动条的步骤
思路很简单,就是利用cookie记录用户当前的document.all.scrollTop,然后在页面onload的时候读出cookie赋值给body.scrollTop
<%@ Page Language="C#"%>
<script runat="Server">
void Page_load(object sender, EventArgs e)
{
btn.Attributes["onclick"] = "document.cookie = /"scrollTop=/" + document.body.scrollTop + /"scrollTop_end/";";
}
void btn_click(object sender, EventArgs e)
{
ArrayList x = new ArrayList();
if (ViewState["myList"] != null)
{
x = (ArrayList)ViewState["myList"];
}
x.Add(DateTime.Now.ToString() + "," + DateTime.Now.AddMinutes(1).ToString());
ViewState["myList"] = x;
for(int i=0;i<x.Count;i++)
{
TableRow tr = new TableRow();
TableCell tc1 = new TableCell();
TableCell tc2 = new TableCell();
string[] str = x[i].ToString().Split(',');
tc1.Text = str[0];
tc2.Text = str[1];
tr.Cells.Add(tc1);
tr.Cells.Add(tc2);
tbl.Rows.Add(tr);
}
}
</script>
<html>
<script>
function setScroll()
{
var getCookie = document.cookie.substring(document.cookie.indexOf("scrollTop") + "scrollTop".length + 1, document.cookie.length - document.cookie.indexOf("scrollTop_end"));
document.body.scrollTop = getCookie;
}
</script>
<body onload="setScroll()">
<form runat="Server">
<asp:Table id=tbl runat=server border=1>
<asp:TableRow>
<asp:TableCell>列1</asp:TableCell>
<asp:TableCell>列2</asp:TableCell>
</asp:TableRow>
</asp:Table>
<asp:button id="btn" runat="Server" text="btn" onclick="btn_click"/>
</form>
</body>
</html>
本文地址:http://www.45fan.com/dnjc/70159.html