Is it possible to call a C# function in my codebehind from javascript?
Asked
Active
Viewed 2,648 times
3
-
You might just use WebServices and AJAX calls... – Yves M. Oct 26 '10 at 07:22
-
possible duplicate of [Can you call C# function from javascript?](http://stackoverflow.com/questions/3994150/can-you-call-c-function-from-javascript) – Shoban Oct 26 '10 at 07:23
2 Answers
4
Yes, you could use PageMethods.

Darin Dimitrov
- 1,023,142
- 271
- 3,287
- 2,928
-
Another link to the PageMethods... http://sappidireddy.wordpress.com/2008/03/31/how-to-call-server-side-function-from-client-side-code-using-pagemethods-in-aspnet-ajax/ – Yves M. Oct 26 '10 at 07:22
-
Thanks @Darin. This looks like the way too go. I have one issue though, i'm getting a javascript error stating that PageMethods is not recognised. Could you think of something I am doing wrong? – Yo Momma Oct 26 '10 at 07:48
1
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

Shoban
- 22,920
- 8
- 63
- 107
-
This is a copy of my answere here ;-) http://stackoverflow.com/questions/3994150/can-you-call-c-function-from-javascript/3994170#3994170 – Shoban Oct 26 '10 at 07:23