1

I am calling an static web method, the method is written in an aspx.cs file having two public classes:

public class Employee
{
    public string EmployeeNumber;
    public string FullName;
    public string LoginName;
    public string EmailID;
    public string Phone;
}

public partial class CustomWebMethods : LayoutsPageBase
{
    [WebMethod]
    public static List<Employee> GetEmployeeDetails(string employeeLoginName)
    {
        List<Employee> lstEmployeeDetail = new List<Employee>();
        //do something
        return lstEmployeeDetail;
    }
}

If I keep the public class employee in the same page then ajax call is working fine.

But if I move the employee to another class library project and add reference of that project, ajax call is not working. Why?

Javascript method not able to call the web method at all.

1 Answer 1

1

This is because of WebMethod attribute internal works. When you mark some method with this attribute, it will be available through PageName.aspx/MethodName url.

Then, after moving this method to outer library, you moving it from the page methods too, and after that it's not available.

So, if you want to refactor your code, you have to add the WebService to your project, from that class you can call methods from other library.

You can also create a javascript proxy on your client like this:

<asp:ServiceReference InlineScript="true" Path="~/CustomersService.asmx"/>

Or use the ScriptManager for it, like this:

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

After that you can use this proxy instead of ajax call.

<script type="text/javascript">
function Add()
{
   var x = $get("txtX").value;
   var y = $get("txtY").value;
   PageMethods.Add(x, y, OnWSAdd);
}

function OnWSAdd(result)
{
   $get("spanAddResult").innerHTML += result;
}
</script>

Old, but great article about Understanding ASP.NET AJAX Web Services.

Sign up to request clarification or add additional context in comments.

2 Comments

I am not sure if I got you correctly, but as far as i understand I am not moving the function(GetEmployeeDetails) that I am calling from javascript, I just moved the employee class which the function(GetEmployeeDetails) is using
@Mac No, I'm not talking about the javascript. I'm talking about that you can't just move the method as it is a part of the page, accessible via javascript.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.