简体   繁体   中英

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. SelectedValue has red underline error inside the 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.

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.

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. Therefore the C# code will not have access to "selectedValue".

so, C# code, gets run on the server, helps generate the HTTP response sent back to the client. JS code, gets run on client once it has the response.

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.

The choice of which method depends on :-

1) how much data there is ( small, do it in js, large, ajax)

2) if any of the data needed for the calculation is sensitive and you don't want the user to see it. (always use ajax to avoid exposing sensitive data)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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