How to use Atlas as an Ajax callback library...

[This topic is extremely out of date.  MS Ajax Extensions have been released and they are different from the Atlas CTPs.  I don't believe Atlas is even available anymore] 

JAAJAX was my little Ajax library that enabled Ajax callbacks.  One of the things I’ve been trying figure out is how to use Atlas to as a replacement for my library.  I originally thought that Atlas could only talk to web services and was corrected by Scott Guthrie; in the comment on the post Scott mentioned that Ajax calback was possible.

I’ve looked all over the net for a simple example.  None exists that I could find (Ok, there is a forum post in the atlas forums of asp.net that told me how (note I have yet to go through any of the Atlas labs)

I have been able to somewhat piece together how to do it.  I’m missing 1 detail, but I will publish what I know (and what I think).

Let’s start with the method in the codebehind -

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Web.Services;

public partial class _Default : System.Web.UI.Page
{

  protected void Page_Load(object sender, EventArgs e)
  {
  }

 

  [WebMethod()]
  public int Add(int num1, int num2)
  {
     
return num1 + num2;
  }
}

Yep, it’s the classic addition Ajax sample (I always start with this because it’s easy to set up, so you can debug it).  Note that you use the WebMethod attribute which is also used with WebServices.  The difference is that page’s codebehind is NOT a web service.  I gave you full the source of the codebehind so you can see that this page is very simple, and it’s definitely not a web service (although it does use the System.Web.Services namespace for the WebMethod attribute). 

So here’s the aspx page’s source:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<
head runat="server">
<title>Untitled Page</title>
</
head>

<body>
<form id="form1" runat="server">
 
<atlas:ScriptManager ID="ScriptManager1" runat="server" />
<div>
 
<input type="text" ID="tb1" /><br />
 
<input type="text" ID="tb2" /><br />
 
<button id="MyButton" onclick="MyButtonClick()">=</button><span id="result"></span>
</div>
</form>

<script>
  function
MyButtonClick()
  {
     
PageMethods.Add($("tb1").value, $("tb2").value, Add_Complete, Add_Timeout, Add_Error);
  }
 
function Add_Complete()
  {
    
if(arguments.length >0)
        $(
"result").innerText = arguments[0].toString();
  }
 
function Add_Timeout()
  {
     alert(
"timeout");
  }
 
function Add_Error(result, response)
  {
     alert(
"Error");
     // One of these 2 objects has the following methods (I forget which one):
     //
get_statusText() gives error text
    
// get_statusCode() gives error code
  }
</script>
</body>
</
html>

So here’s what’s important.  I really only have 1 server tag on this page: the Atlas ScriptManager.  Not only that but we really don’t have to do much with the ScriptManager (just drop it on the form). I have a form consisting of 2 input textboxes, a span, and a Button element (using the Button tag… I love the button tag… sorry I had to use it)…

The onClick event of the Button calls PageMethods.Add.  Hey that’s cool!  There’s an object containing all our server side methods.  I was planning on doing this with JAAJAX.  So what’s up with the parameters for the PageMethods.Add function.  We pass it the 2 parameters for the add function (btw, you might want to do a bit more validation than I did <grin />.  The additional 3 functions are callbacks.  There’s really 3 outcomes for our call to the server: success, timeout (taking too long), and error.  Atlas requires that you pass in all three functions, so it can notify you asynchronously what happened.

The completed function passes back the result and a second object.  I think this object is the Atlas Client WebRequest object that was used to service your request.

[UPDATE: I missed something earlier... that I think is fairly important!  $ is an Atlas (and Prototype, and others) shortcut for document.getElementById function]

The only real thing lacking is synchronous call backs (well, I bet they’re possible, but not desirable).

Final Thoughts
I discovered something else (in both my source reading/note taking as well as through debugging this example).  Atlas will automatically attach itself to your javascript objects[update: because the Atlas client library adds features to the existing Javascript function object].  So, you really don’t have to do anything special (GetName, GetType, etc. methods get added to your functions).  Atlas probably does this via the  parse function (but I’m not 100% sure).  It’s actually pretty cool!

[tags: Ajax, Atlas]

Print | posted on Thursday, April 27, 2006 2:50 PM

Feedback

# re: How to use Atlas as an Ajax callback library...

left by at 4/27/2006 7:13 PM Gravatar

Cool stuff - thanks for posting this sample.

One minor correction above -- the last two methods on the callbacks are optional.  So if you want you can just pass in the completion event method -- and not the timeout or error one.  The last two are there if you want to segment out your error handling logic but are totally optional.

Hope this helps,

Scott

# re: How to use Atlas as an Ajax callback library...

left by at 4/27/2006 7:22 PM Gravatar

Of course it does!  Thanks Scott... I'm just trying to help you guys out with a few more examples...

# re: How to use Atlas as an Ajax callback library...

left by at 4/27/2006 11:37 PM Gravatar

How much javascript is loaded down to do this though?  The library is gigantic. I've been playing with Atlas too and for simple ajax use cases (like this or things like just having an autocomplete textbox) it seems like a major drag.  How does JAAJAX compare in terms of library weight?

# re: How to use Atlas as an Ajax callback library...

left by at 4/27/2006 11:56 PM Gravatar

You bring up a different matter.  In this case you are not dropping the whole Atlas library (239k).  The attributes of the ScriptManager control which JS scripts you download.  The AtlasRuntime.JS which is the trimmed down version is about 56k in it's release form; remember too that a script will be cached.  In era with more and more broadband, I don't think this is an unreasonable size.  I also suspect that Atlas is probably going to get smaller yet.

# re: How to use Atlas as an Ajax callback library...

left by at 4/28/2006 6:59 PM Gravatar

Very nice sample.

So, can you add complex types as parameters and the library will generate compatible jscript classes?

# re: How to use Atlas as an Ajax callback library...

left by at 4/28/2006 7:09 PM Gravatar

You know Joe, I have no idea!

I'll work up a sample and try it... I'll let make a second post regarding this.

Jay

# re: How to use Atlas as an Ajax callback library...

left by at 5/4/2006 8:39 AM Gravatar

  I think that all that you posted is really good but I've been improving on comparison between AjaxPro.NET vs Atlas technologies, and I have a question. Are you sure what are you sending when you causes a POST to a server?

  Using Sys.Net.ServiceMethod.invoke sentence you only sends a JSON data, but when you use a PageMathods sentence, you are sending ALL THE PAGE to the server.

  To make sure of that you can use IE HTTP Analyzer or Fiddler application

  This is a very important issue because in AjaxPro.NET all post that you cause to the server you are sending only JSON data and no more.

  I don't know why Microsoft ONLY USE web services to comunicate to server as a efficiently way. Why I can't communicate whit any WebMethod on any class like AjaxPro.NET??

  I like A lot Atlas because is a big Framework integrated on VS2005, and makes more easy development, but I think that there are very important issues that are not resolved yet.

  P.D. Yes, In atlas you can pas from/to the server customized classes and HTML controls.

  Thanks!!!

   See you soon!!!

# re: How to use Atlas as an Ajax callback library...

left by at 5/4/2006 12:42 PM Gravatar

Benjamin,

I really want to be able to chose.  Mainly because there are times when it is nice to get all the form values as they currently set so you can do some interraction that is more in line with the user's context.

When I was working on getting AutoComplete to work with a PageMethod (theruntime.com/.../143846.aspx), I found that Atlas only sends the Arguments in JSON to make a service call, so it is definitely possible.  And in fact Scott Guthrie responded to the original post and he told me that I shouldn't have to go through that much effort to make it work.  Unfortunately you do (Current release of Atlas --IMO-- appears to be broken in this area... unless I'm missing something... even at that it will be fixed soon, I'm sure (or I'll be corrected)).

But to the point it is possible to send just the JSON params.  

# re: How to use Atlas as an Ajax callback library...

left by at 5/10/2006 9:10 PM Gravatar

Thanks for the great sample. I have put together a few samples of my own and really like the WebMethod feature. However, I need to place my WebMethod in a custom control, but have only been able to get the webmethod call to work when it is in the page itself. Is there a way to implement a webmethod in a webcontrol? It would be great to be able to put my code in a control for reusability.

Thanks.

# re: How to use Atlas as an Ajax callback library...

left by at 5/11/2006 12:01 PM Gravatar

bcarlson,

I would say that you can't put WebMethods in a control.  I would say that you could put them in a base page class, but the control doesn't actually get called back.  If you had some way of raising an event from the control you might be able to raise an event or do your own custom postback within an updatepanel.  At least that's the scope of my knowledge at this point (I need to spend some time investigating building my own extender as well as my own Atlas server controls... you may want to investigate this as well)

Jay

# re: How to use Atlas as an Ajax callback library...

left by at 6/21/2006 6:28 AM Gravatar

hi

I used your code and tried to run that but I am gettting an error of javascrpit error that is "Webmethod is undefined." How to over come this.

please reply

Rupesh kumar Tiwari

# re: How to use Atlas as an Ajax callback library...

left by at 6/21/2006 7:12 PM Gravatar

rupesh,

On the javascript side of things you would be using the PageMethods object not WebMethod.  WebMethod is an attribute that is used to tag your codebehind methods.  It's also used in WebServices (MS is re-using the attribute with Atlas)

# re: How to use Atlas as an Ajax callback library...

left by at 8/11/2006 4:16 PM Gravatar

hi, its simple and nice example

thanks you for make it

i face the following problem with it, when i run the script through clicking the button it act as it didnt recugnise the web method in the behind code and it allways return a null argument

any idea for solving this problem

# re: How to use Atlas as an Ajax callback library...

left by at 8/29/2006 10:02 PM Gravatar

usercontrol?

# re: How to use Atlas as an Ajax callback library...

left by at 8/29/2006 10:04 PM Gravatar

Sorry, Is it possible to to call usercontrol methods?

# re: How to use Atlas as an Ajax callback library...

left by at 8/30/2006 3:38 PM Gravatar

t,

In the next week or so I'll be able to answer that question...

J

# re: How to use Atlas as an Ajax callback library...

left by at 10/27/2006 6:13 AM Gravatar

Hi, can you help me how to use Pagemethods by using new Microsoft.net Ajax beta1.

when i use the same code with <asp:ScriptManager> i got the javascirpt error  PageMethods is undefined.

thanks in advance

# re: How to use Atlas as an Ajax callback library...

left by at 10/27/2006 10:50 AM Gravatar

Check this site later... I'll probably be republishing a bunch of articles once I have time to play with the new beta(s) myself.  They may have moved PageMethods to the CTP (which would annoy me).

# re: How to use Atlas as an Ajax callback library...

left by at 10/27/2006 10:51 AM Gravatar

Check this site later... I'll probably be republishing a bunch of articles once I have time to play with the new beta(s) myself.  They may have moved PageMethods to the CTP (which would annoy me).

# re: How to use Atlas as an Ajax callback library...

left by at 11/14/2006 8:07 AM Gravatar

Hi.

What if the webMethod contains Out Paramters ??

Would the ScriptManager work properly ??

Please, if you know a solution for this , email me at msj_911@hotmail.com.

Thanks in Advance.

# re: How to use Atlas as an Ajax callback library...

left by at 11/14/2006 10:35 AM Gravatar

when i add script manger in my ajax enable site there are a error.

"Sys.Invalid Operation Object Microsoft already exitx and is not a namespace"

Why it is

Please reply

# re: How to use Atlas as an Ajax callback library...

left by at 2/7/2007 8:31 AM Gravatar

hello,

i get the error like &quot;Sys is undefined&quot;. i restarted the iis but nothing cured. any idea what might be causing this.

thanks.

# re: How to use Atlas as an Ajax callback library...

left by at 2/7/2007 1:51 PM Gravatar

Looking at all the places this is listed I really need to either update this thing or make it clear.  This tutorial is very outdated!  It used the MS Atlas CTP (which I don't believe is even available anymore).  

Arif,

You should look at something a little more current... Check out the MS Ajax Extensions docs... BTW, page callbacks are a lot different in MS Ajax Extensions as well.

# re: How to use Atlas as an Ajax callback library...

left by at 6/7/2007 8:15 PM Gravatar

"PageMethods is undefined" happens when the javascript proxy for the .NET server side method was not generated. This in turn can be caused by the method not being declared as public static or not decorated with [WebMethod] Attribute.

'Sys is undefined' Can occur when there are no ajax references in web.config. Create new ajax enabled application in VS and look at the generated web.config.

# re: How to use Atlas as an Ajax callback library...

left by at 6/7/2007 8:27 PM Gravatar

julek,

Did you miss that this is a very old post and you should look elsewhere (in other words it no longer applies).

Check the docs for calling a Web Service... PageMethods are mentioned in that section of the docs.

Jay

Title  
Name
Email (never displayed)
Url
Comments   
Please add 3 and 7 and type the answer here:
div>