How can I pass a C# variable value to javascript to work with amcharts?
-
29-06-2021 - |
Question
I am trying to pass a variable with a json string from C# to javascript where I am trying to use that information to draw an amchart column chart. However, when I try to access the variable that stores the json string from javascript, I receive the following error:
Compiler Error Message: BC30451: 'sjson' is not declared. It may be inaccessible due to its protection level.
at line,
var chartData = '<%=sjson%>';
Here is all of my code:
C#:
public partial class Default3 : System.Web.UI.Page
{
public string sjson;
protected void Page_Load(object sender, EventArgs e)
{
ArrayList conRc = new ArrayList();
for (int i = 0; i < 10; i++)
{
conRc.Add(new Confidence(i.ToString(), (i + 10).ToString()));
}
foreach (object obj in conRc)
{
Confidence cnLv = (Confidence)obj;
Response.Write(cnLv.Ip + " - " + cnLv.Count.ToString());
Response.Write("</br>");
}
System.Web.Script.Serialization.JavaScriptSerializer oSerializer = new System.Web.Script.Serialization.JavaScriptSerializer();
sjson = oSerializer.Serialize(conRc);
}
}
HTML:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>amCharts examples</title>
<link rel="stylesheet" href="http://localhost/amcharts/images/style.css" type="text/css">
<script src="http://localhost/amcharts/amcharts.js" type="text/javascript"></script>
<script type="text/javascript">
var chart;
var chartData = '<%=sjson%>'; //[{ "Ip": "0", "Count": "10" }, { "Ip": "1", "Count": "11" }, { "Ip": "2", "Count": "12" }, { "Ip": "3", "Count": "13" }, { "Ip": "4", "Count": "14" }, { "Ip": "5", "Count": "15" }, { "Ip": "6", "Count": "16" }, { "Ip": "7", "Count": "17" }, { "Ip": "8", "Count": "18" }, { "Ip": "9", "Count": "19"}];
AmCharts.ready(function () {
// SERIAL CHART
chart = new AmCharts.AmSerialChart();
chart.dataProvider = chartData;
chart.categoryField = "Ip";
chart.startDuration = 1;
// AXES
// category
var categoryAxis = chart.categoryAxis;
categoryAxis.labelRotation = 90;
categoryAxis.gridPosition = "Count";
// value
// in case you don't want to change default settings of value axis,
// you don't need to create it, as one value axis is created automatically.
// GRAPH
var graph = new AmCharts.AmGraph();
graph.valueField = "Count";
graph.balloonText = "[[category]]: [[value]]";
graph.type = "column";
graph.lineAlpha = 0;
graph.fillAlphas = 0.8;
chart.addGraph(graph);
chart.write("chartdiv");
});
</script>
</head>
<body>
<div id="chartdiv" style="width: 100%; height: 400px;"></div>
</body>
</html>
Solution
You seem to have missed something very fundamental here.
The first line of an aspx have should look like this:
<%@ Page Language="C#" Inherits="Default3 " Codebehind="Default3.aspx.cs" %>
If you don't have that line, your aspx page will not use the same class, and you can't access its public properties.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow