简体   繁体   English

如何在引用C#函数的Google可视化中包含JavaScript变量

[英]How to include javascript variable in google visualization referencing c# function

There is an error when trying to include the javascript variable "selectedValue" in the c# function being called. 尝试在要调用的c#函数中包含javascript变量“ selectedValue”时出错。 SelectedValue has red underline error inside the GetTotalLicensesCount. SelectedValue在GetTotalLicensesCount内部有红色下划线错误。

function drawExpireGauge() {

    var selectedValue = "30days";

        var data = google.visualization.arrayToDataTable([
              ['Label', 'Value'],
              ['Expiring', <%=GetTotalLicensesCount(null, null, null, selectedValue, selectedValue, null, null)%>],
              ['New', <%=GetTotalLicensesCount(null, null, null, null, null, null, selectedValue)%>],
              ['Not Used', 0]
        ]);
}

if "30days" is simply placed inside the GetTotalLicensesCount function instead of the variable, it works. 如果将“ 30days”简单地放置在GetTotalLicensesCount函数而不是变量中,则它可以工作。

I did due-diligence and searched around the forum for a similar question but couldn't find any, please let me know how the question should be changed or if there is a reasonable duplicate that answers the question. 我进行了尽职调查,并在论坛上搜索了类似的问题,但是找不到任何问题,请让我知道该如何更改该问题,或者是否有合理的副本可以回答该问题。

The C# code doesn't get executed as part of the js execution. C#代码不会在js执行过程中执行。

The injected C# code you have is part of a template that will generate the js code that will eventually get executed on the client. 您注入的C#代码是模板的一部分,该模板将生成最终将在客户端上执行的js代码。 Therefore the C# code will not have access to "selectedValue". 因此,C#代码将无法访问“ selectedValue”。

so, C# code, gets run on the server, helps generate the HTTP response sent back to the client. 因此,C#代码在服务器上运行,有助于生成发送回客户端的HTTP响应。 JS code, gets run on client once it has the response. JS代码在收到响应后立即在客户端上运行。

You have a couple of options, Ajax back to your server to get the data, or provide all the data to js so you can write a js version of GetTotalLicensesCount. 您有两个选择,Ajax返回服务器以获取数据,或将所有数据提供给js,以便您可以编写GetTotalLicensesCount的js版本。

The choice of which method depends on :- 哪种方法的选择取决于:-

1) how much data there is ( small, do it in js, large, ajax) 1)有多少数据(小,用js,大,ajax完成)

2) if any of the data needed for the calculation is sensitive and you don't want the user to see it. 2)如果计算所需的任何数据都是敏感数据,并且您不希望用户看到它。 (always use ajax to avoid exposing sensitive data) (始终使用ajax避免暴露敏感数据)

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

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