Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I server AJAX calls using JSON with Web Forms?

I know MVC controllers are quite good at serving JSON formatted AJAX requests, but is there any built-in ASP.NET functionality I can use for these type calls from a plain old web forms site?

like image 209
ProfK Avatar asked Mar 02 '10 06:03

ProfK


1 Answers

You could use built-in ASP.NET AJAX.

Option 1 - use a web service (if you want the functionality to be reusable):

  • create a web service (.asmx) with [ScriptService] attribute,
  • add a to your page and add the web service to its Services collection,
  • use JavaScript proxy generated by ScriptManager in yor page.

Option 2 - use page methods (if you want the functionality on a single page without creating a web service):

  • define static methods in your page, add [WebMethod] attribute to them,
  • add a ScriptManager with EnablePageMethods="true",
  • use PageMethods object to call these method from JavaScript.

In either case JSON will be used for data transfer.

Here is an extensive tutorial with some code samples.

However, ASP.NET AJAX is often blamed for inefficiency - for instance, JS it generates tends to be rather large. So, if you are concerned with performance, you'd want to test it thoroughly.

You might also have a look at this thread: .NET AJAX Calls to ASMX or ASPX or ASHX?

like image 121
VladV Avatar answered Sep 28 '22 08:09

VladV