Atlas Page Methods

Published Friday, October 07, 2005 9:05 PM

Want to access a method within your page asynchronously using the Atlas framework? Its easy.

Mark your method with the '[WebMethod]' attribute like so:

// Server side code within your .ASPX file or .ASPX.CS code beside file.
[WebMethod]
public string SomeMethod()
{
   return DateTime.Now.ToLongTimeString();

}

In your .ASPX file, you need to include the Atlas javascript file:

<atlas:Script ID="Script1" runat="server" Path="~/ScriptLibrary/Atlas.js" />

The method proxy will be generated by the Atlas framework within the 'PageMethods' namespace and so you can call it like so:
(Note: The 'callback' function is specified as part of the method argument list. In this case its the OnRequestComplete function).

<script type="text/javascript">
  // Calls the 'SomeMethod' server side code asynchronously.

  function
CallThePageMethod() {
    PageMethods.SomeMethod(OnRequestComplete);
  }

  // This function is the callback function that gets executed when the server side code is complete
  // and passes in the result of the server side code as the 'arg' argument.

  function
OnRequestComplete(arg) {
    alert("Result of callback was: " + arg);
  }

</script>

Additionally, if you wanted specify arguments in your server side code, and have the client side asynchronous call specify those arguments, and also define a function to be called if an error occurs, you can simply specify those arguments within the initial call like so:

<script type="text/javascript">
  // Calls the 'SomeMethod' server side code asynchronously.

  function
CallThePageMethod() {
    PageMethods.SomeMethod("myArgument1", "myArgument2", OnRequestComplete, OnRequestError);
  }
</script>

In this and a previous post, I have dealt mostly with the raw scripting part of the Atlas framework. Atlas is much much more than that, and in future posts, I will discuss the aspects the various other components of the Atlas framework. Namely, the declarative scripting, and the server side controls.

by Glav

Comments

# Fabrice said on Friday, October 07, 2005 8:36 AM

This reminds of something!
http://metaSapiens.com/PageMethods

# Michael D. said on Monday, October 10, 2005 8:56 AM

Completely the same as Ajax.NET

# Paul Glavich said on Monday, October 10, 2005 7:50 PM

Identical concept, absolutely, however this is only a small part of Atlas. I did want to show how Atlas does things like AJAX.NET in this case for those familiar with it. I'll show different methods and other features of Atlas in later posts.

# Jeorge Lukasing said on Sunday, August 27, 2006 3:30 AM

Very many thanks for a good work. Nice and useful. Like it!

# Manish said on Tuesday, December 19, 2006 8:13 AM

Hey Glav,

Thanks for the article, It helped me a lot in my project.

Wants to ask, is it possible to call more than one server side method in one client side function using PageMethods.

Basically I want to call the same function thrice in my one JS function. Tell me if it is possible.

Thanks

Leave a Comment

(required) 
(required) 
(optional)
(required) 

This Blog

Syndication