sukesh
sukesh

Reputation: 2425

Scroll page after post back

I would like to scroll the page down after post back. The code doesn't work with / without the Update Panel.

Error in Browser console:

Uncaught SyntaxError: Unexpected token < VM465:1

When clicked on VM465:1, it points to this line:

<script type="text/javascript">$('html, body').animate({ scrollTop: 1600 }, 'slow');</script>

ASPX:

<asp:UpdatePanel ID="up1" runat="server">
    <Triggers>
        <asp:AsyncPostBackTrigger ControlID="btnMore" EventName="Click" />
    </Triggers>
    <ContentTemplate>
<asp:LinkButton ID="btnMore" runat="server" OnClick="btnMore_Click">ShowMore</asp:LinkButton>
</ContentTemplate>
</asp:UpdatePanel>

C#:

protected void btnMore_Click(object sender, EventArgs e)
    {
        //string message = "alert('Hello!')";
        //ScriptManager.RegisterClientScriptBlock(sender as Control, this.GetType(), "alert", message, true);
        ScriptManager.RegisterClientScriptBlock(sender as Control, this.GetType(), "ScrollPage", GetPageScrollScript(900), true);
    }

private string GetPageScrollScript(int heightToScroll)
    {
        string ScrollPage = "<script type=\"text/javascript\">$('html, body').animate({ scrollTop: " + heightToScroll + " }, 'slow');</script>";
        return ScrollPage;
    }

The alert works when I uncomment (wihth and without updatepanel).
What should be done to make the scroll work.

UPDATE: It worked for me this way:

ScriptManager.RegisterClientScriptBlock(sender as System.Web.UI.Control, this.GetType(), "ScrollPage", "$('html, body').animate({ scrollTop: " + 1000 + " }, 'slow')", true);

Upvotes: 0

Views: 207

Answers (1)

Janne Matikainen
Janne Matikainen

Reputation: 5121

You are using the alert like

alert('Hello!')

And that is working, but for the script you want to execute you wrap it around tags

<script type=\"text/javascript\">$('html, body').animate({ scrollTop: " + heightToScroll + " }, 'slow');</script>

try without them?

$('html, body').animate({ scrollTop: " + heightToScroll + " }, 'slow')

Upvotes: 1

Related Questions