Restore the position of the scroll bar after page return (PostBack) _ ax

Source: Internet
Author: User
[Summary]
It involves the DTD template standard, which is annoying and has to be determined, Only passed the test in IE browser.
This is where vs2008 pages all have this, and will affect the style and script:
<! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

You can call the restorescroll method directly in page_load. For details, see Code Note.
[Code] Using System;
Using System. Data;
Using System. configuration;
Using System. LINQ;
Using System. Web;
Using System. Web. Security;
Using System. Web. UI;
Using System. Web. UI. htmlcontrols;
Using System. Web. UI. webcontrols;
Using System. Web. UI. webcontrols. webparts;
Using System. xml. LINQ;

/**/ /// <Summary>
///Summary of ax
/// </Summary>
Public   Class Ax
{
Public Ax ()
{
}

Public   Static   Void Restorescroll (page)
{
// Register a hidden filed
Page. clientscript. registerhiddenfield ( " Hidden_ax " , " 0 " );
System. Text. stringbuilder sb =   New System. Text. stringbuilder ();
// When the scroll bar is rolled, it is saved to the top position of the page in the hidden field.
SB. appendline ( " Function getpagescroll () " );
SB. appendline ( " { " );
SB. appendline ( " VaR yscroll; " );
SB. appendline ( " If (document.doc umentelement. scrolltop) // page compliant with the DTD Standard " );
SB. appendline ( " { " );
SB. appendline ( " Yscroll = document.doc umentelement. scrolltop; " );
SB. appendline ( " } " );
SB. appendline ( " Else " );
SB. appendline ( " { " );
SB. appendline ( " Yscroll = Document. Body. scrolltop; " );
SB. appendline ( " } " );
SB. appendline ( " Return yscroll; " );
SB. appendline ( " } " );
SB. appendline ( " Function savescrollposition () " );
SB. appendline ( " { " );
SB. appendline ( " Document. getelementbyid ('den den _ ax '). value = getpagescroll (); " );
SB. appendline ( " } " );
SB. appendline ( " Window. onscroll = savescrollposition; " );
Page. clientscript. registerstartupscript (page. GetType (), " Ax " , SB. tostring (), True );

// Restore the position of the scroll bar after the page is loaded.
If (Page. ispostback)
{
SB. Remove ( 0 , SB. Length );
SB. appendline ( " Function setscrollposition () " );
SB. appendline ( " { " );
SB. appendline ( " Window. scrollto (0, "   + Page. request [ " Hidden_ax " ] +   " ); " );
SB. appendline ( " } " );
SB. appendline ( " Document. Body. onload = setscrollposition; " );
Page. clientscript. registerstartupscript (page. GetType (), " Axzhz " , SB. tostring (), True );
}
}
}

Call Public   Partial   Class _ Default: system. Web. UI. Page
{< br> protected void page_load ( Object sender, eventargs E)
{< br> ax. restorescroll (PAGE);
}
}

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