[英]How can I pass a C# variable value to javascript to work with amcharts?
我试图将一个带有json字符串的变量从C#传递给javascript,我试图使用该信息绘制一个amchart柱形图。 但是,当我尝试从javascript访问存储json字符串的变量时,我收到以下错误:
编译器错误消息:BC30451:未声明'sjson'。 由于其保护级别,它可能无法访问。
在线,
var chartData = '<%=sjson%>';
这是我的所有代码:
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>
你似乎错过了一些非常基本的东西。
aspx的第一行应该如下所示:
<%@ Page Language="C#" Inherits="Default3 " Codebehind="Default3.aspx.cs" %>
如果您没有该行,则您的aspx页面将不会使用相同的类,并且您无法访问其公共属性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.