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.