Asp.Netページスクロールバーの位置を保持(ページコミット後も変わらない)

3044 ワード

一般的に多くの人がブラウザのスクロールバーを制御するのはPageです.SmartNavigation=Falseは、ページ内のDIVなどのスクロールバー制御について、他の人のコードを見て、参考までに1つ書きました.
 
  
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 = "" _ <br>& "function saveScrollPosition() {" _ <br>& " if (document.all['" & divID(i) & "'] != undefined) {" _ <br>& "document.forms[0]." & divID(i) & "__SCROLLPOS.value = " _ <br>& "document.all['" & divID(i) & "'].scrollTop + ',' " _ <br>& " + document.all['" & divID(i) & "'].scrollLeft;}}" _ <br>& "if (document.all['" & divID(i) & "'] != undefined) {document.all['" & divID(i) & "'].onscroll=saveScrollPosition;}" _ <br>& ""
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 = "" _ <br>& setPositionFunction _ <br>& ""
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.forms[0].' + divID + '__SCROLLPOS').value;
a=e.split(',');
document.getElementById(divID).scrollTop= a[0];
document.getElementById(divID).scrollLeft= a[1];
}
}
}

ページのバックグラウンドで呼び出すときは、次のコードを書きます.
 
  
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
call SaveDivScrollPosition("div1,div2,div3",me)
call RestoreDivScrollPosition("div1,div2,div3",me)
End Sub

申し訳ありませんが、この2つのSUBは1つに書くことができます.参考にしてください.