·您的位置: 首页 » 资源教程 » 编程开发 » ASP » 保持页面中滚动条的位置,使得页面提交后滚动条还是在原来的位置

保持页面中滚动条的位置,使得页面提交后滚动条还是在原来的位置

类别: ASP教程  评论数:0 总得分:0
一般来说很多的人在控制浏览器滚动条都是用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可以写成一个,仅供参考。
-= 资 源 教 程 =-
文 章 搜 索
关键词:
类型:
范围:
纯粹空间 softpure.com
Copyright © 2006-2008 暖阳制作 版权所有
QQ: 15242663 (拒绝闲聊)  Email: faisun@sina.com
 纯粹空间 - 韩国酷站|酷站欣赏|教程大全|资源下载|免费博客|美女壁纸|设计素材|技术论坛   Valid XHTML 1.0 Transitional
百度搜索 谷歌搜索 Alexa搜索 | 粤ICP备19116064号-1