VladN
VladN

Reputation: 739

ASP.NET Ajax with and without Telerik

I'm new to ASP.NET (I'm a PHP developer) and I'm trying to understand how to use AJAX in ASP.NET.

In PHP was simple: Create an async request to an PHP page and the response is placed in a div.

But in ASP.NET how it should be done? Create a async request to an aspx page and place the logic in the Load event? Useing an ASP.NET Handler/ASP.NET Module?

What about the Telerik Ajax? I've seen that in an RadAjaxManager you specify the controller that makes the request, the controller that will be modified by the response but I don't see where to put the logic, how to return a response...

The telerik demos were to complex and I didn't understood anything. I'm simply not able to understand how telerik ajax works because I don't see the things that I'm seeing when programming ajax in javascript...

thanks!

Upvotes: 0

Views: 656

Answers (1)

Niels
Niels

Reputation: 49919

How I use Ajax within ASP.NET is just like PHP, the only thing is I call a webmethod. This way the Ajax call does not have to go through the whole process with all the init, load, prerender, etc. This is way faster then doing a call to a normal ASP.NET page.

My experience with Telerik is that it's really slow and bad for your performance if you want a simple Ajax call.

Below I give a simple example how you can do it:

For C#

public partial class _Default : Page 
{
  [WebMethod]
  public static string GetDate()
  {
    return DateTime.Now.ToString();
  }
}

For VB:

Imports System.Web.Services

Partial Class Default
    Inherits System.Web.UI.Page

    <Script.Services.ScriptMethod()> _
    <WebMethod()> _
    Public Shared Function getDate() As String
        return DateTime.Now.ToString()
    End Function

End Class

Your Javascript:

$.ajax({
    url : "Default.aspx/getDate",
    data : null, // Needs to be a String!, see URL below
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success : function(data){
        var result = data.d; // ASP.NET gives a .d object to the client
        // result = your date, but if you return a SortedList, you can use result.date, or result.html or whatever.
    }
});

For more information:

  1. Ajax (Webmethod): http://encosia.com/using-jquery-to-directly-call-aspnet-ajax-page-methods/
  2. JSON Stringify: https://github.com/douglascrockford/JSON-js

Upvotes: 2

Related Questions