3

我正在尝试将我的代码集成到 Google Chart API。我想从后面的代码中手动设置谷歌图表标题和轴列名称。我尝试了下面的代码,但不起作用。有什么建议么?

ASPX 文件

<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
    <script type="text/javascript">
        google.load("visualization", "1", { "packages": ["corechart"] });
        function drawProgrammingChart() {
            var data = new google.visualization.DataTable(operationdetails, 0.5);
            var chart = new google.visualization.LineChart(document.getElementById("divprog"));
            **var xAxis = document.getElementById("txtXAxisName");**
            **console.log(xAxis);**
            var yAxis = document.getElementById("txtYAxisName");
            chart.draw(data, {
                title: "Visualization Satisfaction", hAxis: { title: "Programming method" }, vAxis: { title: "Units" }
            });
        }
        google.setOnLoadCallback(drawProgrammingChart);
    </script>
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
    <div id="divprog" style="width: 750px; height: 350px;">
        </div>
    <asp:HiddenField ID="txtXAxisName" runat="server" />
    <asp:HiddenField ID="txtYAxisName" runat="server" />
</asp:Content>

在后面的代码中,我想手动设置隐藏字段值。

protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                DataTable dt = generateDataTable();
                **txtXAxisName.Value = "Days";**
                txtYAxisName.Value = "Duration";
                LoadChart(dt, "operationdetails");    
            }

        }
private void LoadChart(DataTable table, string jsName)
        {
            Page.ClientScript.RegisterStartupScript(
                this.GetType(),
                jsName, string.Format("var {0} = {1};", jsName, new Bortosky.Google.Visualization.GoogleDataTable(table).GetJson()), true);
        }

获取元素 txtAxisName 时,它​​为空。

4

4 回答 4

0

您可以尝试以下方法:

var yAxis = document.getElementById('<%# txtYAxisName.ClientID %>');
于 2012-11-18T17:20:37.910 回答
0

首先要获得您需要使用的控件的渲染 id,<%=txtYAxisName.ClientID%>您的代码上的这将是:

var xAxis = document.getElementById("<%=txtXAxisName.ClientID%>");

但要真正获得它的价值,您需要使用 .value 属性。

var xAxis = document.getElementById("<%=txtXAxisName.ClientID%>").value;

但是,如果您实际上不需要更改,也可以在回发此隐藏字段时使用,您可以直接将代码编写为:

<script type="text/javascript" src="https://www.google.com/jsapi"></script>
    <script type="text/javascript">
        google.load("visualization", "1", { "packages": ["corechart"] });
        function drawProgrammingChart() {
            var data = new google.visualization.DataTable(operationdetails, 0.5);
            var chart = new google.visualization.LineChart(document.getElementById("divprog"));
            var xAxis = <asp:Literal runat="server" id="txtXAxisName" EnableViewState="False" />
            var yAxis = <asp:Literal runat="server" id="txtYAxisName" EnableViewState="False" />
            chart.draw(data, {
                title: "Visualization Satisfaction", hAxis: { title: "Programming method" }, vAxis: { title: "Units" }
            });
        }
        google.setOnLoadCallback(drawProgrammingChart);
    </script>
</asp:Content>

和后面的代码

txtXAxisName.Text = "\"Days\";";
txtYAxisName.Text = "\"Duration\";";

并且实际上直接在脚本内部渲染页面上的值。

于 2012-11-18T17:53:23.303 回答
0
var yAxis = document.getElementById('<%# txtYAxisName.ClientID %>');
于 2014-10-08T20:48:49.800 回答
-1

正如 Cal279 所示,我查看了呈现的页面的源代码,如下所示:

<input type="hidden" name="ctl00$MainContent$txtXAxisName" id="MainContent_txtXAxisName" value="Days" />

然后我在下面尝试并成功了。感谢 Cal279 :)

var xAxis = document.getElementById("MainContent_txtXAxisName");
于 2012-11-18T18:37:11.670 回答