1、在aspx页面给body添加id属性:<body id="thebody" MS_POSITIONING="GridLayout">
2、后台代码(.cs文件)中引用:using System.Text;
3、添加方法:
#region "防止页面滚动"
private void RetainScrollPosition()
{
StringBuilder saveScrollPosition = new StringBuilder ();
StringBuilder setScrollPosition = new StringBuilder ();
RegisterHiddenField("__SCROLLPOS", "0");
saveScrollPosition.Append("<script language='javascript'>");
saveScrollPosition.Append("function saveScrollPosition() {");
saveScrollPosition.Append(" document.forms[0].__SCROLLPOS.value = thebody.scrollTop;");
saveScrollPosition.Append("}");
saveScrollPosition.Append("thebody.onscroll=saveScrollPosition;");
saveScrollPosition.Append("</script>");
RegisterStartupScript("saveScroll", saveScrollPosition.ToString());
if (Page.IsPostBack)
{
setScrollPosition.Append("<script language='javascript'>");
setScrollPosition.Append("function setScrollPosition() {");
setScrollPosition.Append(" thebody.scrollTop = " + Request["__SCROLLPOS"] + ";");
setScrollPosition.Append("}");
setScrollPosition.Append("thebody.onload=setScrollPosition;");
setScrollPosition.Append("</script>");
RegisterStartupScript("setScroll", setScrollPosition.ToString());
}
}
#endregion
4、在page_load中调用此方法:
this.RetainScrollPosition();