views:

1139

answers:

8

Can you do ajax on ASP.net webform without using ajax toolkit? (Please post a example link)

+4  A: 

yes... see my example in this post

http://stackoverflow.com/questions/85500/ajax-how-to-pass-value-back-to-server#86520

This can perform much better than ASP.NET ajax, but does take longer to code.

StingyJack
It may take longer to get started, but not once you have a good library for yourself. Plus when you control the library, there's less restrictions on what you can do.
Kibbee
A: 

Certainly you can! What I did in .NET 1.1 (before ASP.Net Ajax was released) was to define an HttpHandler to listen on calls to ajax.aspx, run the appropriate methods, and spit back out the result. I used the Prototype library to make the Ajax call, though you could use any other, or do it by hand.

I'm going by memory, but here's the code I used (for .NET 2.0, but you get the idea):

// client-side js:
var foo = new Ajax.Request('ajax.aspx',
{
   method:'get',
   parameters: { method: 'GetFive' },
   onSuccess: function(transport){
     var response = transport.responseText || "no response text";
     alert("Success! \n\n" + response);
},
onFailure: function(){ alert('Something went wrong...') }
});

// web.config:
<httpHandlers>
  <!-- pre existing handlers go here -->
  <add verb="GET" path="ajax.aspx" type="Fully.Qualified.Name.AjaxHandler, AssemblyName" validate="false" />
</httpHandlers>

// AjaxHandler.cs
public class AjaxHandler : IHttpHandler {
  internal delegate object AjaxFunction();

  private Dictionary<string, AjaxFunction> functions;

  public bool IsReusable {
    get { return true; }
  }

  public void ProcessRequest(HttpContext context) {
    this.functions = new Dicionary<string, AjaxFunction>();
    this.functions.Add("GetFive", delegate() {
      return 5;
    });

    string functionName = context.Request["method"];
    AjaxFunction func = this.functions[functionName];
    if (func != null) {
      object val = func();
      context.Response.Write(val);
    }
  }
}

[Big note: the above is untested and not really designed well, and may not even compile, but it should give you the right idea.]

swilliams
+1  A: 

Sure. People have been doing ajax for years before the term ajax was even coined. Jquery is a popular library that you can use to make calls back to the server without a page refresh.

Ely
A: 

This is a follow up question. Is it possible to use plain html/javascript without ASP.net server control. (Not ASP.net MVC).

Jack
Yes, you can do that using XmlHttpRequest object and javascript. No server controls required.
DOK
A: 

Here's a popular tool: AjaxPro.

As others have noted, you can code the entire thing yourself using XmlHttpRequest objects and javascript. But if your app will be using very sophisticated techniques, writing the code yourself becomes quite tedious. The tools have become much more powerful and easy to use.

Parenthetically, there are two elements to ASP.Net Ajax. One is the Ajax Extensions that are included in Visual Studio 2008 and can be added to Visual Studio 2005. This can be used for much more functionality than the "toolkit". Things like partial page refreshing and retrieving data without performing a full postback. The other is the Ajax Control Toolkit, which is a separate download. The "toolkit" primarily includes fancy controls, especially ones with DHTML effects (show, hide, mimic animation).

DOK
A: 

Oh absolutely.

There are libraries such as jQuery that you can use in your ASP.NET forms instead of UpdatePanel and all the paraphernalia that surrounds it.

Where I used to work we were doing AJAX long before there was a word for it. This was in the days when our stuff only used to work in IE :-)

One thing you need to do in ASP.NET forms where you're using ASP.NET server controls is use the correct client ID that the server generates for your controls when referencing them from javascript. So if you have a label with an ID of say 'telephoneNumber', you need to reference it as <%=telephone.ClientID %>. e.g.

$("#<%=telephoneNumber.ClientID %>").attr("disabled", "disabled");

With libraries such as jQuery you can still call your ASP.NET webservices but you're probably better of investigating WCF and JSON serialisation.

You don't have to throw away everything in the ASP.NET ajax bits, I still make calls to web services using script services. It's a half way house until I can replace with JSON:

http://msdn.microsoft.com/en-us/magazine/cc163499.aspx

Kev
A: 

Ajax just means while user is looking at your web page, with javascript, go back to the server and fetch something else. Then update the page with what you fetched, using javascript, the DOM.

The XMLHttpRequest object is nice, but even before it, you could do the same thing using a hidden IFRAME....

Here is all the javascript code you need to get started:

function GetXmlHttpObject()
{
 var objXMLHttp=null
 if (window.XMLHttpRequest)
 {
  objXMLHttp=new XMLHttpRequest()
 }
 else if (window.ActiveXObject)
 {
  objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP")
 }
 return objXMLHttp
}


function stateChanged()
{
 if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
 {
  // do something with xmlHttp.responseText
 }
}


function SendAsyncHttpRequest()
{
 xmlHttp=GetXmlHttpObject()

 if (xmlHttp==null)
 {
  return
 }

 var url = "http://YOUR_URL"
 xmlHttp.onreadystatechange=stateChanged
 xmlHttp.open("GET",url,true)
 xmlHttp.send(null)
}
Corey Trager
A: 

YES!

There are a "gazillion" alternatives to ASP.NET AJAX out there. Most of them are better in fact too. You might want to check out my project; Ra-Ajax for your needs :)

Thomas Hansen