Asp.net保持页面滚动条位置(页面提交后不变)

弃我而去而又重返的人,我会把他忽略。因为他再也给不了我一颗完整的心。将来的一面,我们必须容忍爱人的所有面。
一般来说很多的人在控制浏览器滚动条都是用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.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

不好意思,这两个SUB可以写成一个,仅供参考。

以上就是Asp.net保持页面滚动条位置(页面提交后不变)。只有正直、忠诚、宽容和拥有仁爱之心的人才能够达到真正意义上的成功。不具备这些品质的人,就无法体会到成功的滋味,因为成功和幸福一样,不在于外在的富有,而在于内心的感受。更多关于Asp.net保持页面滚动条位置(页面提交后不变)请关注haodaima.com其它相关文章!

标签: 滚动条