monkey_boys
monkey_boys

Reputation: 7348

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

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?

Upvotes: 8

Views: 29454

Answers (5)

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);" />

Upvotes: 2

Edgar Hernandez
Edgar Hernandez

Reputation: 4030

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);

Upvotes: 11

Tod
Tod

Reputation: 2524

One thing missing from the answers here is a delay. It's all well and good if there is no change in the height of the web page during load. But if there are a lot of images and your trying scroll past them; they can bump your view back up again.

What is needed is a window.onload function call:

ClientScript.RegisterStartupScript(GetType(), "ScrollScript", "window.onload = function() {document.getElementById('objectid').scrollIntoView(true);}", true);

Upvotes: 0

ram
ram

Reputation: 11626

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).

Upvotes: 14

MikeW
MikeW

Reputation: 5922

Create an anchor on the page, then on onload:

window.location.href = "#myanchor"

Upvotes: 0

Related Questions