简体   繁体   English

使用JavaScript从ascx页面调用ascx.cs方法

[英]Calling ascx.cs method from ascx page using javascript

I have a code behind file which contains a method: 我在文件后面有一个包含方法的代码:

public string ProductsAsJson() 公共字符串ProductsAsJson()

which returns a json representation of a series of products. 它返回一系列产品的json表示形式。 I want to be able to use some angular functions in my ascx page and so I am trying to call the function using: 我希望能够在我的ascx页面中使用一些角度函数,因此我尝试使用以下函数调用该函数:

 <script type="text/javascript">

   var products = <%# ProductsAsJson() %>;

but it is failing to retrieve the list of products. 但是无法检索产品列表。

What am I a doing wrong? 我做错了什么?

您可以将ProductsAsJson()的返回值存储在公共变量中,并在.cs页面加载时调用此方法,并在ascx控件中访问变量的值并分配给javascript变量。

var products = <%# ProductsAsJson %>;

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

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