日期:2009-08-15  浏览次数:20869 次

一般来说很多的人在控制浏览器滚动条都是用Page.SmartNavigation = False,对于页面中DIV等等的滚动条控制,我在看了别人代码后,写了一个,仅供参考:

 Public Shared Sub SaveDivScrollPosition(ByVal divIDArray As String, ByVal objPage As Page)


        Dim saveScrollPosition As String
        Dim i As Integer
        Dim divID() As String

        divID = divIDArray.Split(",")

        For i = 0 To divID.Length - 1
            objPage.RegisterHiddenField(divID(i) & "__SCROLLPOS", objPage.Request.Form(divID(i) & "__SCROLLPOS"))

            saveScrollPosition = "<script language='javascript'>" _
                & "function saveScrollPosition() {" _
                & " if (document.all['" & divID(i) & "'] != undefined) {" _
                & "document.forms[0]." & divID(i) & "__SCROLLPOS.value = " _
                & "document.all['" & divID(i) & "'].scrollTop + ',' " _
                & " + document.all['" & divID(i) & "'].scrollLeft;}}" _
                & "if (document.all['" & divID(i) & "'] != undefined) {document.all['" & divID(i) & "'].onscroll=saveScrollPosition;}" _
                & "</script>"
            objPage.RegisterStartupScript("saveScroll" & divID(i), saveScrollPosition)
        Next
    End Sub

Public Shared Sub RestoreDivScrollPosition(ByVal divIDArray As String, ByVal objPage As Page)
        Dim restoreScrollPosition As String
        Dim setPositionFunction As String
        Dim i As Integer
        Dim divID() As String

        divID = divIDArray.Split(",")

        For i = 0 To divID.Length - 1
            setPositionFunction = setPositionFunction & "SetScrollPosition('" & divID(i) & "');"
        Next
        restoreScrollPosition = "<script language='javascript'>" _
            & setPositionFunction _
            & "</script>"
        objPage.RegisterStartupScript("restoreScroll", restoreScrollPosition)
    End Sub

javascript:

function SetScrollPosition(divID){
    var e;
    var a;
    var obj;
   if (document.getElementById(divID)){
      obj = eval('document.forms[0].' + divID + '__SCROLLPOS');

  if (obj) {
   e=eval('document.