Asp.net keeps the position of the page scroll bar the same after the of page is submitted

  • 2020-06-01 09:32:14
  • OfStack

1 generally speaking, a lot of people use Page. SmartNavigation = False to control the browser scroll bar. For the scroll bar control of DIV in the page, I wrote one after looking at the code of others, just for reference:
 
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]; 
} 
} 
} 

When calling in the background of the page, write the following code:
 
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 

Sorry, these two SUB can be written as 1, just for reference.

Related articles: