Description
Anthem allows you to invoke methods on your server-side pages via client-side JavaScript.
In this example, you'll invoke a simple Add method defined on your
server-side page from the client.
Example
Steps
-
Add a public method called Add to your page so that it takes in two
integers and returns their sum:
public int Add(int a, int b) { return a + b; }
-
Apply the Anthem.Method attribute to the Add method. Without
this, it can't be invoked from clients:
[Anthem.Method]
public int Add(int a, int b)
{
return a + b;
}
-
Register the page with the Anthem manager when the page fires its Load
event:
void Page_Load() { Anthem.Manager.Register(this); }
-
Add three input controls and a button to trigger the call back to your page:
<input id="a" size="3" value="1" />
<input id="b" size="3" value="2" />
<button onclick="DoAdd(); return false;" type="button">Add</button>
<input id="c" size="6">
-
The button is invoking a client-side function called DoAdd. That function
needs to be defined on the page so that it invokes the server-side Add
method:
<script type="text/javascript">
function DoAdd() {
Anthem_InvokePageMethod(
'Add',
[document.getElementById('a').value, document.getElementById('b').value],
function(result) { document.getElementById('c').value = result.value; }
);
}
</script>
-
The first argument to Anthem_InvokePageMethod needs to be the name
of the method you want to invoke:
Anthem_InvokePageMethod(
'Add',
[document.getElementById('a').value, document.getElementById('b').value],
function(result) { document.getElementById('c').value = result.value; }
);
-
The second argument is the array of parameters for that method:
Anthem_InvokePageMethod(
'Add',
[document.getElementById('a').value, document.getElementById('b').value],
function(result) { document.getElementById('c').value = result.value; }
);
-
The third argument is a function that will get invoked here on the client when the
server-side call back completes:
Anthem_InvokePageMethod(
'Add',
[document.getElementById('a').value, document.getElementById('b').value,
function(result) { document.getElementById('c').value = result.value; }
);
-
The argument to the client-side call back function is a result object. It has a
value and an error property. If an error occurred on the
server, value will be null and error won't be:
Anthem_InvokePageMethod(
'Add',
[document.getElementById('a').value, document.getElementById('b').value,
function(result) { document.getElementById('c').value = result.value; }
);
Remarks
Don't forget to make your method public and put the Anthem.Method attribute
on it!
The conversion from strings to the integers (or whatever the type of the parameters
is on the server) happens on the server. The types of parameters that are supported
is limited to the common "primitive" types (like strings, integers, doubles, and
single-dimensional arrays of those types).
The supported return value types is slightly richer with support for DataSet-related
objects and limited automatic support for other custom types.
The client-side call back function can be defined anonymously as the above example
demonstrates.