After the ASP. Net page is refreshed, the method will automatically scroll to the original location for summary.

Source: Internet
Author: User

After the ASP. Net page is refreshed, the method will automatically scroll to the original location for summary.
This article summarizes three methods to automatically scroll to the original location after refreshing pages implemented by ASP. Net. This method is very simple and practical. If you need it, you can refer to it.

 

 

Three methods are summarized after searching on the Internet:

1. Set the MaintainScrollPositionOnPostback attribute in the Page to true.

A>. The page contains MaintainScrollPositionOnPostback. The default value is false. Set it to true (page level)

 

The Code is as follows:


<% @ Page Language = "C #" AutoEventWireup = "true" CodeBehind = "Default. aspx. cs" MaintainScrollPositionOnPostback = "true" Inherits = "Default. Defa
Ult "%>

 

B>. Set the MaintainScrollPositionOnPostback attribute in the Pages node in the web. config configuration file to true (website-level or directory-level)

If you modify web. config in the root directory of the website, all the pages will be affected. If you modify only the web. config file in a directory, only the pages in this directory will be affected.

Specific Method:

Configure it under the <system. web> node:

 

The Code is as follows:


<Pages maintainScrollPositionOnPostBack = "true"> </pages>

 

C>. On the code page, use the C # Or VB code to set the MaintainScrollPositionOnPostback attribute of the page to true.

 

The Code is as follows:


Page. MaintainScrollPositionOnPostBack = true;

 

Or write it like this

 

The Code is as follows:


This. MaintainScrollPositionOnPostBack = true;

 

2. You can use Jquery to obtain the height of the current position of an element. The specific implementation is as follows:

 

The Code is as follows:


Function setPosition ()
{
Var top = $ ("# element id"). offset (). top ();
$ ("Html, body"). animate ({scrollTop: top }, 1000 );
}

 

3. You can use the anchor, but here you can use flexible processing.

First, obtain the id of the location to be rolled to. For example, you can set an element (<span name = "postion" id = "postion"> </span>). Note: to be in form), and to be set anywhere in form

 

The Code is as follows:


<A href = "# postion" id = "clickLink"> </a>

 

Note: do not include any content in tag a, which can be called at the place of return

 

The Code is as follows:


Page. ClientScript. RegisterStartupScript (this. GetType (), "scroll", "document. getElementById ('clicklink'). click ();", true );

 

This method is actually an event that triggers an element.

The above is all the content of this article. I hope you will like it.

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.