Skip to content Skip to sidebar Skip to footer

Can You Call A C# Function From Javascript?

Possible Duplicate: Call ASP.NET function from JavaScript? 'Can you call a C# function from JavaScript?' was asked by an interviewer. Is it possible? If yes, then how?

Solution 1:

You can, but not directly. You'd have to use an AJAX implementation or write an AJAX call yourself using the XmlHttpRequest.

Solution 2:

You cannot call server-side code ‘directly’ from client-side code. That is because by design, the server side code executes at server side and client side code at the client. However there are some workarounds. To call serverside code from javascript, you will need to use AJAX, and the easiest way out, is to use the ASP.NET AJAX Extensions.

Check this link

How to call Server Side function from Client Side Code using PageMethods in ASP.NET AJAX

Solution 3:

You can call C# functions through JavaScript, but not directly. That is to say, you just can't include the namespace and make a direct call to the function. The request has to go indirectly through an interface at the web server which handles browser requests and then sends them to the functions. There are several ways of doing this

  1. Using REST style services. I've been using this way ever since I shifted to ASP.NET MVC.
  2. Using Web services (Page Methods and Service Classes). Again Ajax is involved and there are special tools from Microsoft to make this easier.

Solution 4:

Yes, you can. Check Page Methods.

Solution 5:

Or you can call a managed C# (or whatever) language function in a Silverlight control by calling through the JavaScript bridge.

This is one of those tricky questions where the answer is "not directly, but with a little help from Ajax or the web page DOM I can, and this is how I do it...".

Post a Comment for "Can You Call A C# Function From Javascript?"