Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
192 views
in Technique[技术] by (71.8m points)

call .cs class file(c#) function from javascript

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

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

1 Reply

0 votes
by (71.8m points)

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) { });


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...