Function not defined error while using asp.net ajax

前端 未结 1 669
渐次进展
渐次进展 2020-11-27 23:50

i am trying to call a web service through asp.net ajax by the following code

namespace MCTS70515AJAX
{
public static class HR
{
    public static int GetEmpl         


        
相关标签:
1条回答
  • 2020-11-28 00:33

    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); }
    }
    
    0 讨论(0)
提交回复
热议问题