asp.net keep the page scroll position (unchanged after page submission) _ Practical Tips

Source: Internet
Author: User
In general, a lot of people in the control browser scroll bar is used page.smartnavigation = False, for the page div and so on the scroll bar control, I read someone else's code, wrote a, for reference only:
Copy Code code as follows:

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=saves crollposition;} " _
& "</script>"
Objpage.registerstartupscript ("Savescroll" & divID (i), savescrollposition)
Next
End Sub

Copy Code code as follows:

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 Code code as follows:

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 the page is called in the background, write the following code:
Copy Code code 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, these two sub can be written as one, for 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.