ASP.net Postback - Scroll to Specific Position

how to maintain scroll position on postback in asp.net c#
maintain scroll position on postback asp.net updatepanel
asp net gridview maintain scroll position on postback
how to maintain scroll position on postback in asp.net using javascript
asp.net scroll to bottom of page
classic asp maintain scroll position on postback
maintain scroll position on postback not working
asp net web forms scroll to top

I have an ASP.net WebForms page that has a lot of content on the top of the screen. It has a link button that will post back to the page and show another section of the page. When the page refreshes, I would like to set focus and scroll down to this section of the page.

I tried doing

txtField.Focus()

in my code behind and it will set focus and try to scroll there, but then scrolls right back to the top. The focus is still on my text box but the position of the screen is at the very top. The Link is at the top of the screen which is causing the postback. I want to scroll to the very bottom of the screen. It does this briefly and then scrolls right back to the top.

I have tried setting

Page.MaintainScrollPositionOnPostback = false;

but that doesn't seem to help either.

Is there some way I can force it to go to a specific position? Is it possible to add an anchor tag to the URL when I postback using a button or link button?

Page.MaintainScrollPositionOnPostBack = true; should take you back to the same position on the screen, but you could use AJAX, or you could use SetFocus() to focus on a specific control after the postback:

http://msdn.microsoft.com/en-us/library/ms178232.aspx

Move to anchor after postback, You can add anchor tags in the URL to move the scroll bar to a specific section of the web page. Example  The idea is to capture the position of the Scroll in a global variable each time the user moves the Scroll, in this way it is known which was the last position and when making the postback the EndRequestHandler event is entered and updated with the last position what the user marked

You can use the code below if you have an anchor for the location:

Page.ClientScript.RegisterStartupScript(this.GetType(), "hash", "location.hash = '#MOVEHERE';", true);

[Solved] Maintain page position after postback, Maintain page position after postback ASP.NET · Javascript · UpdatePanel. , +. I have an anchor tag inside listview that expands/hides a row  smart navigation on, but not EXACTLY to the same scroll position (so user sees it jump from middle to bottom, for example). Also, if the control which caused the postback was outside the scrollable area it always scrolled

In your case I suggest you to keep the default value of Page.MaintainScrollPositionOnPostBack, and use the pure javascript scrolling function

function scrollToDiv()
{
    document.getElementById('yourDiv').scrollIntoView();
}

And call it at the page startup with a little delay of 1ms (pure javascript again)

setTimeout(scrollToDiv, 1);

And finally call it from the C# code behind, with the RegisterStartupScript (js executed after all the page has been loaded) :

ScriptManager.RegisterStartupScript(Page, typeof(Page), "ScrollToADiv", "setTimeout(scrollToDiv, 1);", true);

Like this, it will bypass any asp automatic scrolling

Maintain Scroll Position Of Div On PostBack In ASP.NET, In HTML markup, given below, I have HTML's div with the vertical scrollbar for which we will maintain the scroll position on PostBack, HTML's  The idea is to capture the position of the Scroll in a global variable each time the user moves the Scroll, in this way it is known which was the last position and when making the postback the EndRequestHandler event is entered and updated with the last position what the user marked. This worked for me in Firefox and Google Chrome :)

Page.MaintainScrollPositionOnPostback = true seems to work just fine.

Maintain Or Set Page Scroll Position After Asynchronous Postback , Maintain Or Set Page Scroll Position After Asynchronous Postback In ASP.NET AJAX In that case, in the event of click or change event of a specific field we need to do postback to the server and return to the page which  I have an ASP.net WebForms page that has a lot of content on the top of the screen. It has a link button that will post back to the page and show another section of the page. When the page refreshes, I would like to set focus and scroll down to this section of the page. I tried doing . txtField.Focus()

I've tried Matthieu Charbonnier answer, but it didn't work unless I've added

" window.scrollTo = function () { };" 

as it was suggested in http://gnidesign.blogspot.com.au/2011/06/how-to-maintain-page-scroll-on-postback.html

I've created a helper method, that's working in Chrome,FireFox and IE

public static void ScrollToControl( Page page, string clientId, bool alignToTop)
 {
     //NOTE: if there are more than one call on the page, first one will take preference
     //If we want that last will take  preference, change key from MethodBase.GetCurrentMethod().Name to anchorName
     //recommended in http://gnidesign.blogspot.com.au/2011/06/how-to-maintain-page-scroll-on-postback.html              
     String script = " window.scrollTo = function () { };" + Environment.NewLine;
     script += String.Format("document.getElementById('{0}').scrollIntoView({1});" , clientId, alignToTop.JSToString());
     page.ClientScript.RegisterStartupScript(TypeForClientScript(), MethodBase.GetCurrentMethod().Name, script, true );
     //return script;
 }
 public static string JSToString(this bool bValue)
 {
     return bValue.ToString().ToLower();
 }

Use getElementById('{0}').scrollIntoView is simpler than location.hash , because you don't need to add extra anchor element.

Parameter alignToTop is very convenient to specify do you want to show control at the top or bottom of the screen.

Scrolling to position on page after .NET Postback, I have an ASP.net WebForms page that has a lot of content on the top of the screen. It has a link button that will post back to the page and show  Your scroll position is always preserved upon postback because the @Html.HiddenFor fields store your current scroll and pass it to the model on post. And then, when the page comes up it gets the scrollTop value from the model. At the end your page would behave like webform, everything stays intact.

Maintain Scroll Position On Page Refresh Using ASP.NET or jQuery, NET fun, the page would either preserve it's position after a postback when I'd rather have it jump to the top, or it <asp:LinkButton PostBackUrl="#"></asp:​LinkButton> To go to a certain position on the page after postback: What is the best way to reset the scroll position to the top of page after the an asynchronous postback? The asynchronous postback is initiated from a ASP.NET GridView CommandField column and the ASP.NET update panel Update method is called in the GridView OnRowCommand. My current application is an ASP.NET 3.5 web site.

Maintain Scroll Position of DIV on PostBack in ASP.Net, NET that isn't widely known called MaintainScrollPositionOnPostback, which is a common way of maintaining the scroll position after a Postback. Behind the  In this blog, I will explain how to maintain scroll position of HTML div on PostBack in ASP.NET. HTML's div with the vertical scrollbar gets back to the original position after PostBack and hence to solve this problem, we need to retain its scroll position value, using JavaScript and then again apply to div after PostBack.

Maintain Scroll Position On PostBack Using JavaScript, An HTML DIV with vertical scrollbar gets back to original position after PostBack and hence to solve this problem we need to retain its scroll  To keep (or to say it better, to recover) the page scrollbar position after a postback is definitely a feature any form-containing page should have. This use. ASP.NET - MantainScrollPositionOnPostback for Chrome and most recent browsers. Keep track of the page scroll position after a Postback in a cross-browser compatible way (works in Chrome, Firefox, mobile, etc.).

Comments
  • No. The link is at the top of the screen. I want to scroll to the bottom of the screen. When I postback it does this briefly because I am calling the Focus() method on the control, but then for some reason the it will scroll right back to the top.
  • Use client side script to set focus. That will also take care of scrolling to control.
  • Doesn't seem to work either. It's like there is something on my page that is causing it to scroll to the top every time. Can't figure it out. Is it possible to add an anchor tag to my URL when I post back?
  • The scroll control in asp.net web forms is broken and does not work. MaintainScrollPositionOnPostback is part of this defective system. You need to either set the scroll yourself clientside, wth enough delay to avoid race conditions... or override Microsoft's ScriptResource code. I finally decided that overriding their WebForm_AutoFocus function was the only way to make the page not jump around.
  • What about using an id attribute (anchor tag) for different sections of the page? You could dynamically populate Response.Redirect("yourpagewithanchortagrefernece") after the postback
  • Ended up just using an UpdatePanel, as much as I hate them :(
  • It didn't work for me, look at the ClientScript.RegisterStartupScript(..) solution below that worked for me
  • Yes, Page.MaintainScrollPositionOnPostBack should do that... but it doesn't.
  • @Dismissile This should be marked as the answer, in my opinion. You do not have to reload the page from scratch (so the code can be applied only when certain post back events occur), plus the the answer that was accepted was not used and was stated to be the exact opposite of what was asked for.
  • This worked for me, with setting Page.MaintainScrollPositionOnPostBack = false; in the particular instance (since my page has that set true, generally)
  • This is probably going to encounter race conditions.
  • While this might answer the authors question, it lacks some explaining words and/or links to documentation. Raw code snippets are not very helpful without some phrases around them. You may also find how to write a good answer very helpful. Please edit your answer - From Review