views:

213

answers:

2

I have a web application that will need to access code behind methods as well as javascript methods. 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. 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?

A: 

You mean "code behind" as in server code, right? JavaScript code cannot call server code. Typically, your server code would output JS variables, JS functions or whatever other client code cannot be determined at compile time.

EDIT: To clarify, JS code can certainly POST to the server via Ajax, which is indeed "calling server code." 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.

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.

bmoeskau
One setup being considered, which has been tested to work, is that an onclick event executes a javascript function which then calls a WebMethod in the codebehind for that page. Maybe I'm not understanding what you mean with this post but it seems to be an option?
mwright
Actually it is possible for JavaScript code to do a callback to the server. One way is using the ICallBackEventHandler Interface (http://msdn.microsoft.com/en-us/library/system.web.ui.icallbackeventhandler.aspx)
Tim Goodman
Yes, you can certainly execute posts to the server from JS code using any number of Ajax methodologies. This is not the same as "calling page-behind code from JS" (which is not possible, per se) -- you have to expose methods specifically as Ajax-enabled methods on the server, or use framework functionality built for this purpose as mentioned by @Tim Goodman.
bmoeskau
+1  A: 

Let's talk about calling Server side code from JavaScript. This is performed by using Ajax. You can call web methods in the web services and you can also call Page Methods if you decorate them with special attributes.

The main reason of calling server side from js is for using ajax functionality.

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.

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(). But I would say that most of the time 90%+ you call server side code to perform an action asynchronously using 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. 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. 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. Another scenario can be when you want to open a new popup window after the postback happens.

azamsharp
Would you say that calling server side code from Javascript is used when you desire some sort of Ajax functionality then? Is this the only time or are there other considerations? Also, could you elaborate on your discussion of the second scenario? I understand the how, but when/why would you inject javascript into the page for later invocation?
mwright
I have edited my answer!
azamsharp