如何从JavaScript调用一个C#功能? [英] How to call a C# function from javascript?

查看:100
本文介绍了如何从JavaScript调用一个C#功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要打电话 CsharpFunction code从JavaScript后面。我想下面的code。问题是,条件是否为,编译器会调用 CsharpFunction 反正!我该如何控制?

I want to call CsharpFunction code behind from JavaScript. I tried the code below. The problem is that whether the condition is True or False, the compiler will call CsharpFunction anyway! How I can control that?

JavaScript的code:

JavaScript code:

if (Javascriptcondition > 0) {
   <%CsharpFunction();%>
}

C#code背后:

C# code behind:

protected void CsharpFunction()
{
  // Notification.show();
}

感谢您的帮助!

推荐答案

您可以使用Web方法和Ajax

You Can use Web Method and Ajax

   <script type="text/javascript">             //Default.aspx
       function DeleteKartItems() {     
                 $.ajax({
                 type: "POST",
                 url: 'Default.aspx/DeleteItem',
                 data: "",
                 contentType: "application/json; charset=utf-8",
                 dataType: "json",
                 success: function (msg) {
                     $("#divResult").html("success");
                 },
                 error: function (e) {
                     $("#divResult").html("Something Wrong.");
                 }
             });
         }

  [WebMethod]                                 //Default.aspx.cs
  public static void DeleteItem()
  {
     //Your Logic
  }

这篇关于如何从JavaScript调用一个C#功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆