gpt4 book ai didi

c# - 如何将 C# 变量值传递给 javascript 以使用 amcharts?

转载 作者:行者123 更新时间:2023-12-02 19:23:53 25 4
gpt4 key购买 nike

我试图将带有 json 字符串的变量从 C# 传递到 javascript,我试图使用该信息来绘制 amchart 柱形图。但是,当我尝试从 javascript 访问存储 json 字符串的变量时,收到以下错误:

Compiler Error Message: BC30451: 'sjson' is not declared. It may be inaccessible due to its protection level.

在线,

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 页面将不会使用相同的类,并且您无法访问其公共(public)属性。

关于c# - 如何将 C# 变量值传递给 javascript 以使用 amcharts?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12241756/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com