Signup/Sign In
Ask Question
Not satisfied by the Answer? Still looking for a better solution?

Call ASP.NET function from JavaScript?

Is it possible to call a method I created in ASP with JavaScript's click event?
by

2 Answers

Amit8z4mc
The Microsoft AJAX library will accomplish this. You could also create your own solution that involves using AJAX to call your own aspx (as basically) script files to run .NET functions.

I suggest the Microsoft AJAX library. Once installed and referenced, you just add a line in your page load or init:
Ajax.Utility.RegisterTypeForAjax(GetType(YOURPAGECLASSNAME))


Then you can do things like:
<Ajax.AjaxMethod()> _
Public Function Get5() AS Integer
Return 5
End Function

Then, you can call it on your page as:
PageClassName.Get5(javascriptCallbackFunction);


The last parameter of your function call must be the javascript callback function that will be executed when the AJAX request is returned.
pankajshivnani123
You might want to create a web service for your common methods.
Just add a WebMethodAttribute over the functions you want to call, and that's about it.
Having a web service with all your common stuff also makes the system easier to maintain.

Login / Signup to Answer the Question.