1.函数(参考其它网友,有作变动)
#Region "防止页面滚动"
Private Function RetainScrollPosition()
Dim saveScrollPosition As StringBuilder = New StringBuilder
Dim setScrollPosition As StringBuilder = New StringBuilder
RegisterHiddenField("__SCROLLPOS", "0")
saveScrollPosition.Append("<script language='javascript'>")
saveScrollPosition.Append("function saveScrollPosition() {")
saveScrollPosition.Append(" document.forms[0].__SCROLLPOS.value = mybody.scrollTop;")
saveScrollPosition.Append("}")
saveScrollPosition.Append("mybody.onscroll=saveScrollPosition;")
saveScrollPosition.Append("</script>")
RegisterStartupScript("saveScroll", saveScrollPosition.ToString())
If (Page.IsPostBack) Then
setScrollPosition.Append("<script language='javascript'>")
setScrollPosition.Append("function setScrollPosition() {")
setScrollPosition.Append(" mybody.scrollTop = " & Request("__SCROLLPOS") & ";")
setScrollPosition.Append("}")
setScrollPosition.Append("mybody.onload=setScrollPosition;")
setScrollPosition.Append("</script>")
RegisterStartupScript("setScroll", setScrollPosition.ToString())
End If
End Function
#End Region
2.调用
在page_load事件中调用RetainScrollPosition()
3.其它
将<body>的id命名为“mybody”