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.