How to scroll to bottom of page when postback finish in asp.net?

26,909

Solution 1

You could register the a javascript to move the scroll to the position of some control that you want, like this:

    protected void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack)
        {
            RegisterStartupScript("ScrollScript", "document.getElementById('objectId').scrollIntoView(true);");
        }
    }

Changing the objectId in the script for the Id of the object you want to scroll to.

As noted by Guy Starbuk in the comments, RegisterStartupScript is deprecated in .Net 4 and now the recommended way to register a script is:

 ClientScript.RegisterStartupScript(GetType(), "ScrollScript", "document.getElementById('objectId').scrollIntoVie‌​w(true)", true);

Solution 2

from Hosam Kamel's page

To maintain the scroll position for the large web page you can use on of these methods :

1- use Web.config page section <pages maintainScrollPositionOnPostBack="true" />

: this will maintains the scroll positions for all the web site pages.

2- in the page declaration <%@ Page MaintainScrollPositionOnPostback="true" %> : this will maintains the scroll position for this page only.

3- programmatically from code behind System.Web.UI.Page.MaintainScrollPositionOnPostBack = true; : this will maintains the scroll position for this page only (the same as page declration).

Solution 3

In asp.net web pages you can add an OnClientClick event to the control causing the server post back to scroll the page to the bottom.

<asp:Button ID="MyButton" runat="server" Text="Submit" OnClick="MyButton_Click" OnClientClick="window.scrollTo(0, document.body.scrollHeight);" />
Share:
26,909
monkey_boys
Author by

monkey_boys

Nothing I' am alone boy and be live for my dream Nothing about me Csharp Snippet Piyanut Khajohnsubdee King Mongkut's University of Technology North Bangkok Google's Profile programmatically : Dynamic Style ปิยะณัฐ ขจรทรัพย์ดี

Updated on November 02, 2020

Comments

  • monkey_boys
    monkey_boys over 3 years

    How to scroll to bottom of page when postback finish in asp.net?

    I have many details in page when I click "Show Detail" in master detail, this page show many data in my page. So how to to scroll to bottom of page automatically?

  • monkey_boys
    monkey_boys over 14 years
    How to use its i add <%@ Page MaintainScrollPositionOnPostback="true" %> but not work to bottom?
  • monkey_boys
    monkey_boys over 14 years
    that current scroll position?
  • ram
    ram over 14 years
    sorry, I misunderstood the problem. I had a similar situation where the user would scroll to the bottom of the page, do a post back and would lost the scroll position.Now I understand that the user directly should go to the bottom of the page after postback
  • Guy Starbuck
    Guy Starbuck over 13 years
    This is a great answer and worked for me -- something to note is that RegisterStartupScript is deprecated in .NET 4, I used the following: ClientScript.RegisterStartupScript(GetType(), "ScrollScript", "document.getElementById('ContentPlaceHolder1_postSaveMessag‌​eDiv').scrollIntoVie‌​w(true)", true);
  • Jason R
    Jason R almost 4 years
    Important to note that this seems to be the only one of the proposed solutions that work inside of an updatepanel. This doesn't help the OP but may help somebody coming to this question from google, like me.