简体   繁体   English

使用JavaScript将参数传递给函数背后的代码

[英]Pass parameters to code behind function using JavaScript

I'm trying to call a code-behind function inside JavaScript and pass a parameters to the code-behind function. 我正在尝试在JavaScript中调用代码隐藏函数,并将参数传递给代码隐藏函数。

For example: 例如:

<script> 
var name; 
<% createUser(name) %> 
</script>

private void createUser(string Name)
{ // Do crazy stuff }

I'm doing this because some of the elements are dynamically created using jQuery, so I cannot access them in server side code. 我这样做是因为某些元素是使用jQuery动态创建的,因此无法在服务器端代码中访问它们。 The example above is relatively simple and nothing close to what I'm trying to achieve, nevertheless, it does give you a good overview of my problem. 上面的示例相对简单,并且与我要达到的目标几乎没有任何关系,但是它确实为您提供了有关我的问题的很好的概述。

Thanks! 谢谢!

You can't directly "call" a code-behind function from JavaScript. 您不能直接从JavaScript“调用”代码隐藏函数。 JS runs in the client's browser, and C# is running on a server somewhere else. JS在客户端的浏览器中运行,而C#在其他地方的服务器上运行。 The two communicate using HTTP requests via the medium of a web server. 两者通过Web服务器的介质使用HTTP请求进行通信。 You can fire off an HTTP Request from the client, and have a server page waiting to process such requests by delegating to code-behind functions: 可以从客户端触发HTTP请求,并通过委派给代码隐藏功能让服务器页面等待处理此类请求:

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

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