Step 1: Every server-side method that is called from the client-side, must be declared as "static", and also has to be decorated with the [System.Web.Services.WebMethod] tag.
[System.Web.Services.WebMethod()]
public static string Message()
{
return "Hello from the server-side World!";
}
Step 2: Modifying the ScriptManager The "EnablePageMethods" attribute has to be added on the ScriptManager tag.
<asp:ScriptManager ID="ScriptManager1" runat="server"EnablePageMethods="true" />
Step 3:Adding a simple HTML button
<input onclick="GetMessage()" type="submit" value="Get Message" />
Step 4. Adding the JavaScript code
function GetMessage() {
PageMethods.Message(OnGetMessageSuccess, OnGetMessageFailure);
}
The "OnGetMessageSuccess" is the name of the JavaScript function that will be called if the request is successful. Whereas the "OnGetMessageFailure" will be called if an exception is thrown.
function OnGetMessageSuccess(result, userContext, methodName) {
alert(result);
}
function OnGetMessageFailure(error, userContext, methodName) {
alert(error.get_message());
}