如何从jquery获取返回值到C#类代码

时间:2016-07-04 06:23:59

标签: c# jquery asp.net

我有一个C#课。我希望用C#方法调用jquery方法,返回值为

我有C#class as bellow

public class test
{
public void get()
{
//here the code how to call jquery method with retun values
}
}

我的jquery方法如下所示

<script type="text/javascript">
function displayservices() {

return "hello";
        }

    </script>

1 个答案:

答案 0 :(得分:1)

客户端的Javascript函数即浏览器无法向服务器端函数返回值。你需要ajax调用服务器来传递javascript函数的值。

的Javascript

<script type="text/javascript">

    function displayservices() {
    PageMethods.GetCurrentTime(document.getElementById("Hello", OnSuccess);
    }
    function OnSuccess(response, userContext, methodName) {
      alert(response);
    }

</script>

代码背后。

[System.Web.Services.WebMethod]
public static string ReceiveValueFromJS(string value)
{
     return "Value received " + value
} 

这篇文章Calling ASP.Net AJAX PageMethods using ScriptManager Example将进一步指导您。