简体   繁体   English

将变量从VB.Net传递到Javascript?

[英]Passing a variable from VB.Net to Javascript?

I have some charts that are created client-side using Javascript. 我有一些使用Javascript在客户端创建的图表。 I would like them to use data queried and calculated server-side in my .net codebehind. 我希望他们在背后的.net代码中使用查询和计算的服务器端数据。

Each chart only needs passed 2 numbers, and I suppose a third variable (its name). 每个图表只需要传递两个数字,我想第三个变量(它的名称)。

This is proving harder than I expected as I haven't found a working answer by googling? 事实证明,这比我预期的要难,因为我没有通过谷歌搜索找到可行的答案?

I know I could create some hidden values on the page, but since the chart drawing script is in the head of the document surely it would try to create the charts before reading these values 我知道我可以在页面上创建一些隐藏的值,但是由于图表绘制脚本确实位于文档的开头,因此它将在读取这些值之前尝试创建图表

If you are using ASP.NET MVC and Razor, you could use the following method 如果您使用的是ASP.NET MVC和Razor,则可以使用以下方法
1. Calculate the needed values for the charts in code behind and assign it to model properties. 1.使用后面的代码计算图表所需的值,并将其分配给模型属性。
2. Put Razor code to render chart variables above the chart drawing script. 2.将Razor代码置于图表绘图脚本上方以呈现图表变量。

<!-- Render chart variables -->   
<script type="text/javascript">   
    var chartVariable1 = @Html.Raw(@Model.ChartVariable1);  
</script>
<!-- Chart drawing script --> 

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

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