页面回传(PostBack)后恢复滚动条的位置_AX
【总结】
其中涉及到了DTD模板标准,比较烦人,还得判断下,只在IE浏览器测试通过.
就是这个,VS2008页面都有这个了,会影响样式和脚本:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
在Page_Load里直接调用RestoreScroll方法就OK了,详见代码注释.
【代码】
调用
其中涉及到了DTD模板标准,比较烦人,还得判断下,只在IE浏览器测试通过.
就是这个,VS2008页面都有这个了,会影响样式和脚本:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
在Page_Load里直接调用RestoreScroll方法就OK了,详见代码注释.
【代码】
using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
/// <summary>
///AX 的摘要说明
/// </summary>
public class AX
{
public AX()
{
}
public static void RestoreScroll(Page page)
{
//注册一个Hidden Filed
page.ClientScript.RegisterHiddenField("Hidden_AX", "0");
System.Text.StringBuilder sb = new System.Text.StringBuilder();
//当滚动条滚动时将其到页顶的位置保存在Hidden Field中
sb.AppendLine("function getPageScroll() ");
sb.AppendLine("{ ");
sb.AppendLine(" var yScroll; ");
sb.AppendLine(" if (document.documentElement.scrollTop)//符合DTD标准的Page ");
sb.AppendLine(" { ");
sb.AppendLine(" yScroll = document.documentElement.scrollTop; ");
sb.AppendLine(" } ");
sb.AppendLine(" else ");
sb.AppendLine(" { ");
sb.AppendLine(" yScroll = document.body.scrollTop; ");
sb.AppendLine(" } ");
sb.AppendLine(" return yScroll; ");
sb.AppendLine("} ");
sb.AppendLine("function saveScrollPosition() ");
sb.AppendLine("{ ");
sb.AppendLine(" document.getElementById('Hidden_AX').value = getPageScroll();");
sb.AppendLine("} ");
sb.AppendLine("window.onscroll=saveScrollPosition; ");
page.ClientScript.RegisterStartupScript(page.GetType(), "AX", sb.ToString(), true);
//页面加载完后恢复滚动条的位置
if (page.IsPostBack)
{
sb.Remove(0,sb.Length);
sb.AppendLine("function setScrollPosition() ");
sb.AppendLine("{ ");
sb.AppendLine(" window.scrollTo(0," + page.Request["Hidden_AX"] + "); ");
sb.AppendLine("} ");
sb.AppendLine("document.body.onload=setScrollPosition; ");
page.ClientScript.RegisterStartupScript(page.GetType(), "AXzhz", sb.ToString(), true);
}
}
}
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
/// <summary>
///AX 的摘要说明
/// </summary>
public class AX
{
public AX()
{
}
public static void RestoreScroll(Page page)
{
//注册一个Hidden Filed
page.ClientScript.RegisterHiddenField("Hidden_AX", "0");
System.Text.StringBuilder sb = new System.Text.StringBuilder();
//当滚动条滚动时将其到页顶的位置保存在Hidden Field中
sb.AppendLine("function getPageScroll() ");
sb.AppendLine("{ ");
sb.AppendLine(" var yScroll; ");
sb.AppendLine(" if (document.documentElement.scrollTop)//符合DTD标准的Page ");
sb.AppendLine(" { ");
sb.AppendLine(" yScroll = document.documentElement.scrollTop; ");
sb.AppendLine(" } ");
sb.AppendLine(" else ");
sb.AppendLine(" { ");
sb.AppendLine(" yScroll = document.body.scrollTop; ");
sb.AppendLine(" } ");
sb.AppendLine(" return yScroll; ");
sb.AppendLine("} ");
sb.AppendLine("function saveScrollPosition() ");
sb.AppendLine("{ ");
sb.AppendLine(" document.getElementById('Hidden_AX').value = getPageScroll();");
sb.AppendLine("} ");
sb.AppendLine("window.onscroll=saveScrollPosition; ");
page.ClientScript.RegisterStartupScript(page.GetType(), "AX", sb.ToString(), true);
//页面加载完后恢复滚动条的位置
if (page.IsPostBack)
{
sb.Remove(0,sb.Length);
sb.AppendLine("function setScrollPosition() ");
sb.AppendLine("{ ");
sb.AppendLine(" window.scrollTo(0," + page.Request["Hidden_AX"] + "); ");
sb.AppendLine("} ");
sb.AppendLine("document.body.onload=setScrollPosition; ");
page.ClientScript.RegisterStartupScript(page.GetType(), "AXzhz", sb.ToString(), true);
}
}
}
调用
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
AX.RestoreScroll(Page);
}
}
{
protected void Page_Load(object sender, EventArgs e)
{
AX.RestoreScroll(Page);
}
}
少帮主的斧头好久不饮血了!