5

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?

2

5 Answers 5

7

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

Sign up to request clarification or add additional context in comments.

Comments

4

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

Comments

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.

Comments

1

Yes, you can. Check Page Methods.

Comments

1

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...".

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.