我在ASP .NET中添加动态脚本时遇到问题
我想将脚本放在ASP:ScriptManager中的scriptText中,但是当我运行我的应用程序时它不起作用,有人可以帮助我吗?
这个应用程序用于创建一个包含数据库数据的图表,当我从数据库中读取数据并将其放入scriptText时,一切正常,非常好,但是我看不到图表,如果我放了代码来自chartsJS.org,它运作正常。
PLZ帮助! 转发谢谢!
我有这个脚本
protected void Page_Load(object sender, EventArgs e)
{
List<object> office = new List<object>();
List<object> cantidad = new List<object>();
List<object> labelCantidad = new List<object>();
List<string> labels = new List<string>();
DataTable tablaResultado = new DataTable();
tablaResultado = Datos.AccesoDatos.ResumenOffice();
if (tablaResultado.Rows.Count > 0)
{
foreach (DataRow fila in tablaResultado.Rows)
{
labels.Add(fila[0].ToString());
labelCantidad.Add(fila[1].ToString());
}
}
StringBuilder sb = new StringBuilder();
string scriptText = "";
scriptText += "var ctx = document.getElementById(" + "resumen_office" + ").getContext('2d');";
scriptText += "var myChart = new Chart(ctx, {";
scriptText += "type: 'line',";
scriptText += " data: {";
foreach (object obj in labels)
{
sb.Append(string.Format("'{0}', ",obj));
}
scriptText += "labels: [" + sb + "]";
scriptText += "datasets: [{";
scriptText += "label: 'Microsoft Office',";
StringBuilder objt = new StringBuilder();
foreach (object obj in labelCantidad)
{
objt.Append(string.Format("{0}, ",Convert.ToInt32(obj)));
}
scriptText += "data: [" + objt + "]";
scriptText += " backgroundColor: [";
scriptText += "'rgba(255, 99, 132, 0.2)',";
scriptText += "],";
scriptText += "borderColor: [";
scriptText += "'rgba(255,99,132,1)',";
scriptText += "],";
scriptText += "borderWidth: 1";
scriptText += "}]";
scriptText += "},";
scriptText += "options: {";
scriptText += "scales: {";
scriptText += "yAxes: [{";
scriptText += "ticks: {";
scriptText += "beginAtZero: true";
scriptText += "}";
scriptText += "}]";
scriptText += "}";
scriptText += "}";
scriptText += "});";
scriptText += "</script>";
grafico.CompositeScript.Path = scriptText;
}
此脚本位于tests.aspx.cs
中在tests.aspx中我有这个
<%@ Page Title="" Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="tests.aspx.cs" Inherits="tests" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="CPH" runat="Server">
<div class="container-fluid">
<canvas id="resumen_office"></canvas>
</div>
<asp:ScriptManager ID="grafico" runat="server">
</asp:ScriptManager>
</asp:Content>
答案 0 :(得分:0)
使用ScriptManager.RegisterStartupScript或ScriptManager.RegisterClientScriptBlock使用ScriptManager在页面上注册脚本块。对于包含脚本,请使用ScriptManager.RegisterClientScriptInclude