简体   繁体   English

从C#调用JS函数不起作用

[英]JS function call from C# doesn't work

I try to call the JS function from CodeBehind ( C# ) : 我尝试从CodeBehind(C#)调用JS函数:

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

The function works when I call it directly from my asp.net. 当我直接从asp.net调用该函数时,该函数起作用。 I tried this but it doesn't work... : 我试过了,但是不行...:

 Page.ClientScript.RegisterStartupScript(this.GetType(), "goBot", "goToBottom()", true);

RegisterClientScriptBlock and RegisterStartupScript do not "call" code or functions, they simply a dd code to the page ; RegisterClientScriptBlockRegisterStartupScript不会“调用”代码或函数,它们只是将dd代码添加到页面 RegisterClientScriptBlock adds script to the top of the page—so it might not see all the html because it's not loaded; RegisterClientScriptBlock将脚本添加到页面的顶部,因此可能未看到所有html,因为尚未加载。 RegisterStartupScript adds script to the bottom of the page—so all the html is available to it. RegisterStartupScript将脚本添加到页面底部,因此所有html均可用。

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. 从javascript调用代码是另一个问题。 It has been asked before. 有人问过。 Search the site or start another question. 搜索站点或提出另一个问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM