Function not defined error while using asp.net ajax

You can can try PageMethods, just add the using and the [WebMethod]

using System.Web.Services;

public static class HR
    {
        [WebMethod]
        public static int GetEmployeeCount(string department)
        {
            int count = 0;
            ...
            return count;
        }
    }

In your aspx modify your scriptManager like this

<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true">
</asp:ScriptManager>

then you can call the method in the JS this way

function myJS_method() {
    var departments = $get("Departments"); // your string value
    PageMethods.GetEmployeeCount(departments , onSucess, onError);
    function onSucess(result) {
        // your code when it is OK
        // result is the return value of your C# method
    }
    function onError(result) { alert('Error' + result); }
}

Leave a Comment