回发后

时间:2015-10-29 07:37:42

标签: c# webforms

我想在回帖后滚动页面。代码在使用/不使用更新面板时无效。

浏览器控制台错误:

  

未捕获的SyntaxError:意外的令牌< VM465:1

单击VM465:1时,它指向此行:

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

当我取消注释(没有和没有updatepanel)时,警报会起作用 如何使滚动工作。

更新 它对我有用:

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

1 个答案:

答案 0 :(得分:1)

您正在使用

之类的提醒
alert('Hello!')

这是有效的,但对于你想要执行的脚本,你将它包装在标签

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

没有他们试试?

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