Asp.net maintains the position of the page scroll bar (unchanged after the page is submitted)

Source: Internet
Author: User

In general, many people use pages to control browser scroll bars. smartNavigation = False. For the scroll bar control of DIV in the page, I wrote one after reading other people's code for reference only: Copy codeCode: 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

Copy codeCode: 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:Copy codeThe Code is as follows: function SetScrollPosition (divID ){
Var e;
Var;
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 the page in the background, write the following code:Copy codeThe Code is As follows: 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, the two SUB can be written as one for your reference only.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.