Atlas Page Methods

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.

3 Comments

  • Completely the same as Ajax.NET

  • 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.

  • 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

Comments have been disabled for this content.