Dream Tech Blog

A person with no dream in life can never live a happy life.
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

Persist ScrollBar's Position After a PostBack

Posted on 2004-07-08 14:29  Dream  阅读(995)  评论(3编辑  收藏  举报

这个page的参数可以去掉吗?

Public Shared Sub PersistScrollPosition(ByVal page As Page)

        Dim saveScrollPosition As System.Text.StringBuilder = New System.Text.StringBuilder
        Dim setScrollPosition As System.Text.StringBuilder = New System.Text.StringBuilder

        page.RegisterHiddenField("__SCROLLPOS", "0")

        saveScrollPosition.Append("<script language='javascript'>")
        saveScrollPosition.Append("function saveScrollPosition() {")
        saveScrollPosition.Append(" document.forms[0].__SCROLLPOS.value = document.body.scrollTop;")
        saveScrollPosition.Append("}")
        saveScrollPosition.Append("document.body.onscroll=saveScrollPosition;")
        saveScrollPosition.Append("</script>")

        page.RegisterStartupScript("saveScroll", saveScrollPosition.ToString())

        If page.IsPostBack Then
            setScrollPosition.Append("<script language='javascript'>")
            setScrollPosition.Append("function setScrollPosition() {")
            setScrollPosition.Append(" document.body.scrollTop = " + page.Request("__SCROLLPOS") + ";")
            setScrollPosition.Append("}")
            setScrollPosition.Append("document.body.onload=setScrollPosition;")
            setScrollPosition.Append("</script>")

            page.RegisterStartupScript("setScroll", setScrollPosition.ToString())
        End If

    End Sub