Just add the using
at the beginning and the [WebMethod]
decorator to the static
method to be called in the aspx page:
using System.Web.Services;
public partial class MyPage : System.Web.UI.Page
{
[WebMethod]
public static int GetRandomNumberLessThan(int limit)
{
var r = new Random();
return r.Next(limit);
}
}
In your .aspx file add a asp:ScriptManager enabling Page Methods:
<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true">
</asp:ScriptManager>
Then you can call it from JS like this:
var limit= 42 // your parameter value
PageMethods.GetRandomNumberLessThan(limit, onSuccess, onError);
function onSuccess(result) {
var randomNumber = result;
// use randomNumber...
}
function onError(result) {
alert('Error: ' + result);
}