0

I try to call the JS function from CodeBehind ( C# ) :

function goToBottom() {
        window.scrollTo(0, document.body.scrollHeight);
    }

The function works when I call it directly from my asp.net. I tried this but it doesn't work... :

 Page.ClientScript.RegisterStartupScript(this.GetType(), "goBot", "goToBottom()", true);
Hilai
  • 25
  • 6
  • `make action before in C#`. I have no idea what that means... – VDWWD Mar 09 '18 at 08:23
  • I edited my post – Hilai Mar 09 '18 at 08:27
  • Some things to check: Do you see the script rendered in the source? Have you set a breakpoint on it? Have you checked the browser console for errors yet? – Jeroen Mar 09 '18 at 08:29
  • Yes I see the script in the source and I have set a breakpoint on it but I don't know why it doesn't work. Nothing is displayed in the browser console – Hilai Mar 09 '18 at 08:37
  • But the function is call, I can the that with the breakpoint – Hilai Mar 09 '18 at 08:39
  • From [the docs](https://msdn.microsoft.com/en-us/library/z9h4dk8y(v=vs.110).aspx) it seems that such a startup script is called *before* the page's `onLoad` event. So not sure if scrolling will work at that stage. I suggest trying [a different server side approach: `RegisterClientScriptBlock`](https://stackoverflow.com/a/27207097/419956), or [a client side solution](https://stackoverflow.com/a/800010/419956) as a wrapper for your `goToBottom` call. – Jeroen Mar 09 '18 at 08:54
  • Thank for your answer, I tried with RegisterClientSciptBlock but it don't work to. So I research a client side solution. But when I click on my button I want in the first time execute the "OnClick" ( code Behind ) and in the seconde time the OnClientClick ( JS ) have you got a solution ? – Hilai Mar 09 '18 at 13:40
  • @Hilai did you see my answer? – wazz Mar 16 '18 at 07:19
  • Yes thank you but need to execute the js code after code behind ( C# ) because in c# I make a panel visible, the page will go lower once the panel is displayed – Hilai Mar 20 '18 at 08:18

1 Answers1

0

RegisterClientScriptBlock and RegisterStartupScript do not "call" code or functions, they simply add code to the page; RegisterClientScriptBlock adds script to the top of the page—so it might not see all the html because it's not loaded; RegisterStartupScript adds script to the bottom of the page—so all the html is available to it.

If you want to scroll down when the page loads, take it out of the function:

// this will run the *first time* the page loads.
window.scrollTo(0, document.body.scrollHeight); // no function.

If you want to call it from a click, use a function:

function goToBottom() {
    window.scrollTo(0, document.body.scrollHeight);
}

Calling code behind from javascript is another issue. It has been asked before. Search the site or start another question.

wazz
  • 4,057
  • 5
  • 18
  • 32
  • Also, Jeroen's link to 'different server side approach' might help you (uses jquery). (see comments in original post.) – wazz Mar 10 '18 at 00:46