1

I have created Class under App_Code folder there i have created method. I need to call that class method using JavaScript from Sample.aspx file.

Class:

namespace ContactBook.App_Code
{
    //The function need to call...
    public class ContactBook_functionalities
    {
        public static bool MyFunction(string email, string contact)
            {
                 //Code...
            }
    }
}

JavaScript:

<script type="text/javascript">
        function callMyFunc(email, contact)
        {
             //var x = MyFunction(string email, string contact);
        }
</script>

[WebMethod] is not working for Class functions.

1
  • Hi, please check my answer below. You can press the upvote (upward arrow) if you find the answer helpful. Commented Sep 2, 2015 at 7:25

2 Answers 2

1

I don't thing you can do that because:

  • ASPX is server side, whereas JavaScript is client side
  • some components from a ASP site are compiled

What you can do is expose a route (if you were using MVC desing pattern) on the server that calls that function (or an Api for that mather).

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

1 Comment

In that am not using any MVC desing pattern.
0

You need to expose an HTTP handler in ASP.net or Controllers / Web API in MVC for your JavaScript to have the capability of talking to the server. Simple classes are not accessible outside ASP.net Application via JavaScript.

After creating your own controller/HTTP handler, you need to send an AJAX request to the controller and it should look like this code below:

 $.ajax({
   url: "sampleurlhere",
   type: "POST"//POST or GET,
   data: {
       id: "sample id as parameter"
   },
   success: function(e) {
    //this event fires up when response arrives.
   }
 })

5 Comments

From that am not using any MVC desing pattern, So Controller is not in my Project.
@karthikeyanmlp, you can look on the links I provided on how to use HTTP handlers in ASP.net
How can i call the Handler_function with arguments using JavaScript? I think that the below is only way to call Handler "localhost/HttpHandler/test.sample"
How can i call the Handler_function with FunctionName using JavaScript? I think that the below is only way to call Handler "localhost/HttpHandler/test.sample" And also i have use that Class_methods from CodeBehing file. If am write Handler, for the same operation, I need to write twice. For Class and handler. That is also makes problem on future because i need to update those not a single. Is there no other way to use class_methods directly???
@karthikeyanmlp, please check the code I gave you. Each property of the data object is equivalent of an argument on the handler method

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.