简体   繁体   English

需要在用户单击按钮时同时调用 JavaScript 函数和代码隐藏函数

[英]Need to call a JavaScript function and a code behind function at the same time when a user clicks a button

I am working in a project that has C# code behind and a JavaScript file attached to the aspx.我在一个背后有 C# 代码和一个附加到 aspx 的 JavaScript 文件的项目中工作。

I need to call a function in the JavaScript file and call a function from the code behind, all at the same time when the user clicks a button.我需要在 JavaScript 文件中调用一个函数并从后面的代码中调用一个函数,所有这些都在用户单击按钮时同时进行。

Essentially the JavaScript function is telling a service to save this form while the C# function is going to the next page.本质上,当 C# 函数进入下一页时,JavaScript 函数告诉服务保存此表单。

I tried using onclick and onserverclick attributes for the button but it would only do one or the other (depending on if the on lock attribute returned true or false)我尝试为按钮使用 onclick 和 onserverclick 属性,但它只会执行一个或另一个(取决于 on lock 属性是返回 true 还是 false)

I also tried onbeforeunload and onsubmit to call the JavaScript function separately and it didn't seem to work as nothing happened after the windows.postmessage call.我还尝试过 onbeforeunload 和 onsubmit 分别调用 JavaScript 函数,但它似乎不起作用,因为在 windows.postmessage 调用之后没有任何反应。 (May be related to an iframe issue that I don't know much about) (可能与一个我不太了解的 iframe 问题有关)

Any advice or guidance would be greatly appreciated!任何建议或指导将不胜感激! Thanks谢谢

@Grumpy96 Have you tried onClick for server-side execution and onClientClick for javascript function? @Grumpy96 您是否尝试过 onClick 用于服务器端执行和 onClientClick 用于 javascript 功能? However, the javascript function will trigger first and then server-side function.但是,javascript 函数会先触发,然后再触发服务器端函数。 Hope this helps.希望这可以帮助。

You can trigger javascript with OnClientClick="functionName();"您可以使用OnClientClick="functionName();"触发 javascript

or you can trigger your postback from the jquery like below.或者你可以像下面这样从 jquery 触发你的回发。

        $("buttonSelector").click(function{
           //Do your service call
          $("anotherSelector").trigger('click');
        })

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

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