javascript - Scroll to the Bottom of the Page after postBack - Stack Overflow

I need to Scroll Automatically to the Bottom of the Page when I click on the button. I have found lot o

I need to Scroll Automatically to the Bottom of the Page when I click on the button. I have found lot of solutions on the forum but it don't work for me. I tried this in Java Script :

<asp:Button  ID="btn_add_action1" runat="server" Text="Ajouter une action" onclick="btn_add_action1_Click" OnClientClick = "goToBottom()" />

With the JS function :

window.scrollTo(0,document.body.scrollHeight);

or

document.body.scrollTop = document.body.scrollHeight;

I found this here : Scroll Automatically to the Bottom of the Page

I tried many other solution but it don't work

I need to Scroll Automatically to the Bottom of the Page when I click on the button. I have found lot of solutions on the forum but it don't work for me. I tried this in Java Script :

<asp:Button  ID="btn_add_action1" runat="server" Text="Ajouter une action" onclick="btn_add_action1_Click" OnClientClick = "goToBottom()" />

With the JS function :

window.scrollTo(0,document.body.scrollHeight);

or

document.body.scrollTop = document.body.scrollHeight;

I found this here : Scroll Automatically to the Bottom of the Page

I tried many other solution but it don't work

Share Improve this question edited Jul 18, 2017 at 6:27 baker asked Jul 18, 2017 at 6:07 bakerbaker 351 silver badge10 bronze badges 3
  • do you really need a asp button? try normal html button. now page will postback while click on this button. – Arun Raj Commented Jul 18, 2017 at 6:11
  • Possible duplicate of onClick go to the bottom of page using jQuery .animate – ReadyFreddy Commented Jul 18, 2017 at 6:13
  • I realy need a asp button because it is associated with this function btn_add_action1_Click who execute an sql query... – baker Commented Jul 18, 2017 at 6:14
Add a ment  | 

2 Answers 2

Reset to default 5

When you click the button, a PostBack is performed. That means the scrolling position will be lost. If you want to scroll to the bottom of the page you have to do it after the PostBack is done, by using ScriptManager

protected void Button1_Click(object sender, EventArgs e)
{
    //your button code

    ScriptManager.RegisterStartupScript(Page, Page.GetType(), "scrollDown", "setTimeout(function () { window.scrollTo(0,document.body.scrollHeight); }, 25);", true);
}

There is also something called MaintainScrollPositionOnPostBack, that does something similar, it goes to the same position as the button click after PostBack.

Kindly Put MaintainScrollPositionOnPostback="true" on your Page header <%@ Page %>, it will automatically scroll where you was last time.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745377347a4625061.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信