Step 1: Set EnablePageMethods property as True in Script Manager

<asp:ScriptManager ID=”ScriptManager1″ runat=”server” EnablePageMethods=”True” />

Step2: Write the function in xxxxx.aspx.cs file

[WebMethod]
public static int GetValue()
{
return 2;
}

Step3: if we need to call above function at client side then use the below code

<input type=”button” value=”Submit” onclick=”SubmitData()” />

• Write below functions in javascript file

function OnRequestComplete(result) {
alert(result);
}
function OnRequestError(error) {
if (error != null) {
alert(error.get_message());
}
}

function SubmitData() {
PageMethods.GetValue (OnRequestComplete, OnRequestError);
}

Advertisements