简体   繁体   English

从后面的代码调用 JavaScript 函数

[英]Calling a JavaScript function from code behind

I am calling a JavaScript function from code behind in a button click event handler the issue is that the function executes when the behind code finishes execution, can any one help so that the script function execute at the time I call it ?我正在从按钮单击事件处理程序中的代码后面调用一个 JavaScript 函数,问题是该函数在后面的代码完成执行时执行,有人可以帮助我在调用它时执行脚本函数吗? regards.问候。

Is code behind necessary to create a function dynamically or something like that?动态创建函数或类似的东西是否需要背后的代码? If not you could just make your button not call code behind at all:如果不是,你可以让你的按钮根本不调用代码:

<button onclick="myFunction(event)">Click me</button>

and in并在

function myFunction(e)
{
  e.preventDefault();
}

It will prevent button from making a postback.它会阻止按钮进行回发。 Otherwise if you take out preventDefault() the button would still execute script before executing postback.否则,如果您取出 preventDefault() 按钮,则在执行回发之前仍会执行脚本。 To execute script from code behind before postback code ends running would require some very evil workarounds using HttpResponse.Flush or something.要在回发代码结束运行之前从代码后面执行脚本,需要使用 HttpResponse.Flush 或其他东西的一些非常邪恶的解决方法。

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

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