Keep the position of the scroll bar on the page so that the scroll bar is still in the original position after the page is submitted

Source: Internet
Author: User
Keep the position of the scroll bar on the page so that the scroll bar is still in the original position after the page is submitted

In general, many people use page. smartnavigation = false to control the browser's scroll bar. For the scroll bar control of DIV in the page, I am readingCodeAnd write one for your reference only:

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;
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:

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, you can write one sub 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.