简体   繁体   English

从背后的代码调用javascript与从javascript背后的代码调用

[英]Calling javascript from a code behind vs. calling a code behind from javascript

I have a web application that will need to access code behind methods as well as javascript methods. 我有一个Web应用程序,将需要访问方法以及javascript方法背后的代码。 For this particular implementation it doesn't really matter which order they are called in from a program flow perspective. 对于这个特定的实现,从程序流的角度来看,调用它们的顺序并不重要。

I'm looking for insight into when it would be appropriate to use a code behind to call javascript and when it would be appropriate to call javascript from a code behind. 我正在寻找什么时候适合使用背后的代码调用javascript以及何时适合从背后的代码调用javascript的见解。 Are there any ramifications of doing it one way or the other that I should be aware of before moving forward with an implementation? 在继续实施之前,应该以一种方式或另一种方式进行操作会产生什么影响?

Is there a best practice way to do it or is it very specific to the actual implementation? 是否有最佳做法来做到这一点,或者它是否真的针对实际实施?

Let's talk about calling Server side code from JavaScript. 让我们谈谈从JavaScript调用服务器端代码。 This is performed by using Ajax. 这是通过使用Ajax执行的。 You can call web methods in the web services and you can also call Page Methods if you decorate them with special attributes. 您可以在Web服务中调用Web方法,如果用特殊属性装饰它们,也可以调用Page方法。

The main reason of calling server side from js is for using ajax functionality. 从js调用服务器端的主要原因是使用ajax功能。

Calling js from server side is basically when you inject javascript code into the page and then the client code will be invoked on some action. 从服务器端调用js基本上是将javascript代码注入到页面中,然后在执行某些操作时调用客户端代码。

UPDATE: 更新:

There might be some other reasons also where you need to invoke server side from javascript this can include manually submitting the form using document.form.submit(). 可能还有其他一些原因,您需要从javascript调用服务器端,这可能包括使用document.form.submit()手动提交表单。 But I would say that most of the time 90%+ you call server side code to perform an action asynchronously using ajax. 但是我会说大多数情况下,超过90%的时间您会调用服务器端代码来使用ajax异步执行操作。

For second scenario a common example can be when you want to add a confirmation boxes into the buttons that are contained inside the gridview control. 对于第二种情况,一个常见的示例是您想在gridview控件内的按钮中添加确认框。 In that scenario you will add code in the databound event of the gridview control and add javascript code to the buttons contained in the gridview. 在这种情况下,您将在gridview控件的databound事件中添加代码,并将javascript代码添加到gridview包含的按钮中。 Finally, when the gridview is rendered the buttons inside the gridview will have javascript attached to them and when the user clicks the button it pops up the confirmation box. 最后,当呈现gridview时,gridview中的按钮将附加有javascript,当用户单击该按钮时,将弹出确认框。 Another scenario can be when you want to open a new popup window after the postback happens. 另一种情况可能是发生回发后要打开新的弹出窗口的情况。

You mean "code behind" as in server code, right? 您是指服务器代码中的“代码落后”,对吗? JavaScript code cannot call server code. JavaScript代码无法调用服务器代码。 Typically, your server code would output JS variables, JS functions or whatever other client code cannot be determined at compile time. 通常,您的服务器代码将输出JS变量,JS函数或在编译时无法确定的任何其他客户端代码。

EDIT : To clarify, JS code can certainly POST to the server via Ajax, which is indeed "calling server code." 编辑 :为澄清起见,JS代码当然可以通过Ajax POST到服务器,这实际上是“调用服务器代码”。 I thought you were asking about directly calling code-behind functions from JS since they are "on the same page" in Visual Studio, which is not possible unless you expose the functions as Ajax-enabled and use some client-side Ajax mechanism to call them. 我以为您是在问直接从JS调用代码隐藏函数的问题,因为它们在Visual Studio中“在同一页上”,除非您将这些函数公开为支持Ajax并使用某些客户端Ajax机制进行调用,否则这是不可能的他们。

To best answer your question, specific use cases would be helpful. 为了最好地回答您的问题,特定的用例会有所帮助。 There is not a general right or wrong reason to do this -- depends on what you're trying to accomplish. 这样做没有一般的对与错原因-取决于您要完成的工作。

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

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