Solve the problem of position reset of div scroll bar after return (using hidden control principle) _ Practical skills

Source: Internet
Author: User
Tags net return

The first is to add a onscroll= "Javascript:setscolltop ()" Method for the Div (not necessarily div other tagging ideas should be the same)

Copy Code code as follows:

<div style= "Overflow:auto; height:152px "align=" left "id=" Dvscoll_1 "onscroll=" Javascript:setscolltop () ">

The Setscrolltop method here is to assign the scrolltop value of a div to a hidden control each time the scroll bar scrolls. The code is as follows

Copy Code code as follows:

function Setscolltop ()
document.getElementById ("<%=hidScrollTop.ClientID%>"). Value=document.getelementbyid ("Dvscoll_1"). scrolltop;

Because every. NET postback scroll bar is reset, here we Page_Load the DIV's scrolltop in the back of the text. The code is as follows

Copy Code code as follows:

if (! IsPostBack)
Restore scroll bar position
if (this.hidscrolltop.value!= "")
string scrolltop = This.hidScrollTop.Value;
String strscript= "document.getElementById (' Dvscoll_1 '). scrolltop=" +scrolltop;
Registerscriptblock (this. Page, Strscript);

The Registerscriptblock here is a method I wrote in a common class library, as shown below.

Copy Code code as follows:

Registering script blocks
public static void Registerscriptblock (System.Web.UI.Page Page, String _scriptstring)
Page. Clientscript.registerstartupscript (page. GetType (), "Scriptblock", "<script type= ' Text/javascript ' >" + _scriptstring + "</script>");

Related Article

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