ASP.Net Automatically scrolls to the original location method summary after refreshing the page

  • 2021-06-28 12:19:17
  • OfStack

After searching on the web, three ways are summarized:

1. Set the MaintainScrollPositionOnPostback property in Page to true

- A > .page has MaintainScrollPositionOnPostback, default is false, set to true (page level)


<%@ 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 (site or directory level)

) If you modify web.config in the root directory of your site, all pages will be affected; if you modify only the web.config file in a directory, only the pages in that directory will be affected

Specific measures:

In < system.web > Configuration under node:


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

- C > On the page's code page, set page's MaintainScrollPositionOnPostback property to true through C#or VB code


Page.MaintainScrollPositionOnPostBack = true;

Or write like this


this.MaintainScrollPositionOnPostBack = true;

2. You can use Jquery to get the height of an element's current position as follows


function setPosition() 

     var top=$("# element id").offset().top(); 
     $("html,body").animate({scrollTop:top},1000); 

3. Anchor points can be used, but flexible handling is available here

First get the id where you want to scroll to, for example, you can set an element ( < span name="postion" id="postion" > < /span > , NOTE: To be in form, set it anywhere else in form


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

Note: Do not include anything in the a tag, call it where it is returned


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

This method is actually an event that triggers an element

The above is the whole content of this article, I hope you like it.


Related articles: