Scroll to Top of page on Button Click using JavaScript in ASP.Net

Last Reply 27 days ago By dharmendr

Posted 27 days ago

Hi ,

How to set page scrollbar at "top position" on asp button click event.

I am submitting the some details into the system post that page scrollbar need to set at above(top position). Please advice

You are viewing reply posted by: dharmendr 27 days ago.
Posted 27 days ago

Hi Waghmare,

Use scrollTo() method scrolls the document to the specified coordinates.

window.scrollTo(x, y)

x is the coordinate to scroll along the x-axis (horizontal) in pixels.

y is the coordinate to scroll along the y-axis (vertical) in pixels.

Check this example. Now please take its reference and correct your code.

HTML

<asp:ScriptManager runat="server" />
This is sample text<br />This is sample text<br />This is sample text<br />This is sample text<br />
This is sample text<br />This is sample text<br />This is sample text<br />This is sample text<br />
This is sample text<br />This is sample text<br />This is sample text<br />This is sample text<br />
This is sample text<br />This is sample text<br />This is sample text<br />This is sample text<br />
This is sample text<br />This is sample text<br />This is sample text<br />This is sample text<br />
This is sample text<br />This is sample text<br />This is sample text<br />This is sample text<br />
This is sample text<br />This is sample text<br />This is sample text<br />This is sample text<br />
This is sample text<br />This is sample text<br />This is sample text<br />This is sample text<br />
This is sample text<br />This is sample text<br />This is sample text<br />This is sample text<br />
This is sample text<br />This is sample text<br />This is sample text<br />This is sample text<br />
This is sample text<br />This is sample text<br />This is sample text<br />This is sample text<br />
This is sample text<br />This is sample text<br />This is sample text<br />This is sample text<br />
This is sample text<br />This is sample text<br />This is sample text<br />This is sample text<br />
This is sample text<br />This is sample text<br />This is sample text<br />This is sample text<br />
This is sample text<br />This is sample text<br />This is sample text<br />This is sample text<br />
This is sample text<br />This is sample text<br />This is sample text<br />This is sample text<br />
This is sample text<br />This is sample text<br />This is sample text<br />This is sample text<br />
This is sample text<br />This is sample text<br />This is sample text<br />This is sample text<br />
This is sample text<br />This is sample text<br />This is sample text<br />This is sample text<br />
This is sample text<br />This is sample text<br />This is sample text<br />This is sample text<br />
This is sample text<br />This is sample text<br />This is sample text<br />This is sample text<br />
This is sample text<br />This is sample text<br />This is sample text<br />This is sample text<br />    
<asp:UpdatePanel runat="server">
    <ContentTemplate>
        <asp:Button ID="btnSave" Text="Save" runat="server" OnClick="btnSave_Click" />
    </ContentTemplate>
</asp:UpdatePanel>
<script type="text/javascript">
    function ScrollToTop() {
        window.scrollTo(500, 0);
    }
    //On UpdatePanel Refresh
    var prm = Sys.WebForms.PageRequestManager.getInstance();
    if (prm != null) {
        prm.add_endRequest(function (sender, e) {
            if (sender._postBackSettings.panelsToUpdate != null) {
                ScrollToTop();
            }
        });
    };
</script>

Code

C#

protected void btnSave_Click(object sender, EventArgs e)
{
    // Do your task.
}

VB.Net

Protected Sub btnSave_Click(ByVal sender As Object, ByVal e As EventArgs)
    ' Do your task.
End Sub

Screenshot