方法一:
防止页面滚动#region "防止页面滚动"
public void RetainScrollPosition(System.Web.UI.Page myPage,string strBodyName)
{
StringBuilder saveScrollPosition = new StringBuilder ();
StringBuilder setScrollPosition = new StringBuilder ();
myPage.RegisterHiddenField("__SCROLLPOS", "0");
saveScrollPosition.Append("<script language='javascript'>");
saveScrollPosition.Append("function saveScrollPosition() {");
saveScrollPosition.Append(" document.forms[0].__SCROLLPOS.value = " + strBodyName +".scrollTop;");
saveScrollPosition.Append("}");
saveScrollPosition.Append( strBodyName + ".onscroll=saveScrollPosition;");
saveScrollPosition.Append("</script>");
myPage.RegisterStartupScript("saveScroll", saveScrollPosition.ToString());
if (myPage.IsPostBack)
{
setScrollPosition.Append("<script language='javascript'>");
setScrollPosition.Append("function setScrollPosition() {");
setScrollPosition.Append(" " + strBodyName + ".scrollTop = " + myPage.Request["__SCROLLPOS"] + ";");
setScrollPosition.Append("}");
setScrollPosition.Append(strBodyName + ".onload=setScrollPosition;");
setScrollPosition.Append("</script>");
myPage.RegisterStartupScript("setScroll", setScrollPosition.ToString());
}
}
#endregion
方法二:
Page.SmartNavigation = true;
有可能会造成你页面JavaScript 出错,这是ASP.NET 的一个BUG
方法三:
利用<大眼睛>的原理,我把过程简化了一下,分三部分:
一、
在<form>与</form>之间插入<input type="hidden" name="__SCROLLPOS">,用以记录页面的当前滚动位置;
二、
给<body>起个名字,比如:<body id="theBody">,同时编写body的onscroll事件,总体如下:
<body id="theBody" onload="bodyload()" onscroll="document.all["__SCROLLPOS"].value = theBody.scrollTop;">
三、
在Page_Load事件中加入如下代码(vb.net),作用在于ie显示该页面时,马上重新定位原来的滚动位置:
Dim js As String
js = "<script language=""javascript"">function bodyload() {"
js &= "js &= "theBody.scrollTop = " & CInt("0" & Request.Form("__SCROLLPOS")) & "; "
js &= "document.forms[0].__SCROLLPOS.value = " & CInt("0" & Request.Form("__SCROLLPOS")) & "; "
js &= "}</script>"
Response.Write(js)
注:此文章转载至白衣轩