简体   繁体   English

调用C#方法并从JavaScript传递变量

[英]Call C# method and pass variable from JavaScript

I have this ASP.NET C# method in a user control: 我在用户控件中有此ASP.NET C#方法:

protected void SaveChanges(object clientArr)
{
    System.Diagnostics.Debug.WriteLine("SaveChanges");
    //clientArr will be a string[]
}

I want to be able to call this method and pass a variable form the JavaScript side. 我希望能够调用此方法并从JavaScript端传递变量。 I know doing this '<%=divMainContent.ClientID%>' worked to get a client ID. 我知道这样做'<%=divMainContent.ClientID%>'可以获取客户端ID。 I tried doing '<%=SaveChanges(clientArr)%>' , but it's not working. 我尝试做'<%=SaveChanges(clientArr)%>' ,但是它不起作用。 Is there a working and simple solution for this? 有没有可行且简单的解决方案? Preferably using the '<%=%>' (not sure what it's called)? 最好使用'<%=%>' (不确定它的名称)吗?

JavaScript method: JavaScript方法:

function clickSaveChanges(element) {
    var mainContentChildren = document.getElementById('<%=divMainContent.ClientID%>').childNodes;
    var clientArr = new Array(mainContentChildren.length * 2);
    //add stuff to  clientArr
   '<%=SaveChanges(clientArr)%>'  //this line causes page to crash
}

SaveChanges should return something for the <%= %> notation. SaveChanges应该为<%= %>表示法返回内容。 However, you can't call a server-side method from client-side JavaScript by invoking it by name; 但是,不能通过名称调用它来从客户端JavaScript调用服务器端方法。 you have to communicate with the server by a web service or page callback . 您必须通过Web服务页面回调与服务器进行通信。 Page callback initiates the normal flow of the page lifecycle, even though it is an out of band request. 页面回调启动页面生命周期的正常流程,即使它是带外请求。

You can achieve same thing with very simple ajax call. 您可以通过非常简单的ajax调用来实现相同的目的。 You just need to add JQuery in scripts to use ajax. 您只需要在脚本中添加JQuery即可使用ajax。

C# code C#代码

[WebMethod]
public static void SaveChanges(object clientArr)
{ 
 System.Diagnostics.Debug.WriteLine("SaveChanges");
 //clientArr will be a string[]
}

JavaScript 的JavaScript

 function clickSaveChanges(element) {
var mainContentChildren = document.getElementById('<%=divMainContent.ClientID%>').childNodes;  

  var clientArr = new Array(mainContentChildren.length * 2);
   //add stuff to  clientArr

   String sParam = clientArr.toString(); 
   $.ajax({ 
   type: "POST", 
   url: "PageName.aspx/SaveChanges", 
   data: "{s:sParam}", // passing the parameter 
   contentType: "application/json; charset=utf-8", 
   dataType: "json", 
   success: function(retValue) {
    // Do something with the return value from.Net method
    } 
}); 

} }

You cant mix client side JavaScript code an server side C# code. 您不能混合使用客户端JavaScript代码和服务器端C#代码。 What you need is some AJAX magic to make this work. 您需要一些AJAX魔术来使其正常工作。 You will need 2 things to make this work: 您需要完成以下两项工作:

  1. An endpoint that you can send the request to. 您可以将请求发送到的端点。 You can get this using ASP.NET WebAPI, or some other .NET style endpoint. 您可以使用ASP.NET WebAPI或其他一些.NET样式终结点获取此信息。 That will be dependent on your current architecture. 那将取决于您当前的体系结构。 Are you using MVC or WebForms? 您正在使用MVC还是WebForms? This endpoint will be responsible for calling your SaveChanges() method once it receives the data from your UI. 该端点从您的UI接收数据后,将负责调用SaveChanges()方法。

  2. You will need an AJAX call from the client to send the data to your endpoint. 您将需要客户端进行AJAX调用,以将数据发送到端点。 The easiest way to do this is using jQuery. 最简单的方法是使用jQuery。 Take a look at the documentation for the AJAX method: 查看有关AJAX方法的文档:

http://api.jquery.com/jquery.ajax/ http://api.jquery.com/jquery.ajax/

A super simple example would be something like this: 一个超级简单的示例如下所示:

$.ajax({
type: "POST",
url: "yourEndpointAddress",
data: {
    name: "John",
    location: "Boston"
    }
})
.success(function (msg) {
        alert("Data Saved: " + msg);
    });

Hope it helps. 希望能帮助到你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM