程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> ASP.NET >> ASP.NET基礎 >> Asp.net保持頁面滾動條位置(頁面提交後不變)

Asp.net保持頁面滾動條位置(頁面提交後不變)

編輯: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可以寫成一個,僅供參考。
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved