Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have doubt regarding call .cs class file(C#) function from javascript My Code: I have class file(.cs) like call_cs_function_from_js

------------------------------------------------------------------


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace call_cs_function_from_js
{
    public class cal_lcs_function_from_js
    {
        public void getdata()
        {

        }
    }
}

This is javascript code File:

<script type="text/javascript">
function call(){
  cal_lcs_function_from_js.getdata();  //This way is not working 
  alert('called');
}
</script>

Here I want call getdata of cal_lcs_function_from_js from call()(means .js). call() invoked when button click.

Please Show me what are the other ways.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
191 views
Welcome To Ask or Share your Answers For Others

1 Answer

You can not call your C# function directly from your javascript code. As javascript runs on client side and your C# function resides on the server. For that you have to create a Web Service, and call that service form your javascript using Ajax.

UPDATE:

  1. First add the namespace using System.Web.Services; to your web page.
  2. Add the following method to your page

    [WebMethod]
    public string GetData()
    {
        return ("");
    }
    
  3. Call the method using Ajax.

    $.ajax({ type: "GET", url: "/GetData", success: function (data) { });


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...