当我从同一页面上的javascript函数访问它时,隐藏字段值返回null

时间:2012-05-21 11:23:40

标签: javascript json hidden

我用我的aspx形式编写的内容::

     <%@ Page Title="Sector Composition" Language="C#" MasterPageFile="~/UIPages/ApexM.Master"
     AutoEventWireup="true" CodeFile="Sector_Composition.aspx.cs" Inherits="Anonymous_Sector_Composition" %>

   <asp:Content ID="Content1" ContentPlaceHolderID="head" runat="Server">
   <script type="text/javascript">


    var browserData = [];
    var versionsData = [];
    var chart;



    function OnPageLoad()
    {



        var oJSON_DataTable_DataHolder = document.getElementById("JSON_DataTable_DataHolder");

        var oJSON = eval("(" + oJSON_DataTable_DataHolder.value + ")");

        var colors = Highcharts.getOptions().colors;



        for (var i = 0; i < oJSON.TABLE[0].ROW.length; i++) {


            for (var j = 0; j < oJSON.TABLE[0].ROW[i].COL.length; j++) {



                browserData.push({
                    name: oJSON.TABLE[0].ROW[i].COL[1].DATA,
                    y: oJSON.TABLE[0].ROW[i].COL[2].DATA,
                    color: colors[i].color

                });

                versionsData.push({
                    name: oJSON.TABLE[0].ROW[i].COL[1].DATA,
                    y: oJSON.TABLE[0].ROW[i].COL[4].DATA,
                    color: colors[i].color
                });


            }
        }







        // Create the chart
        chart = new Highcharts.Chart({
            chart: {
                renderTo: 'container',
                type: 'pie'
            },
            title: {
                text: 'Browser market share, April, 2011'
            },
            yAxis: {
                title: {
                    text: 'Total percent market share'
                }
            },
            plotOptions: {
                pie: {
                    shadow: false
                }
            },
            tooltip: {
                formatter: function () {
                    return '<b>' + this.point.name + '</b>: ' + this.y + ' %';
                }
            },
            series: [{
                name: 'Browsers',
                data: browserData,
                size: '60%',
                dataLabels: {
                    formatter: function () {
                        return this.y > 5 ? this.point.name : null;
                    },
                    color: 'white',
                    distance: -30
                }
            }, {
                name: 'Versions',
                data: versionsData,
                innerSize: '60%',
                dataLabels: {
                    formatter: function () {
                        // display only if larger than 1
                        return this.y > 1 ? '<b>' + this.point.name + ':</b> ' + this.y + '%' : null;
                    }
                }
            }]
        });


    }



   </script>


    </asp:Content>
    <asp:Content ID="Content2" ContentPlaceHolderID="cphContent" runat="Server">


   <div id="Container">

   </div>
     <div>

        <asp:HiddenField ID="JSON_DataTable_DataHolder"  runat="server" />

    </div>


  </asp:Content>

在我的代码背后:::

   protected void Page_Load(object sender, EventArgs e)
  {

    psp_Chart_Composition_MktCapTableAdapter composition = new psp_Chart_Composition_MktCapTableAdapter();

    DataTable compositions = composition.GetData();
    JSON_Class Object_JSON_Class = new JSON_Class();
    ///////// JSON String with Rows And Cols////////////////////////////
    JSON_DataTable_DataHolder.Value = Object_JSON_Class.JSON_DataTable(compositions);



   }

代码有什么问题?我无法获得javascript函数中隐藏字段的值。我试过函数调用window.onload = OnPageLoad;以及按钮onclientclick事件。

2 个答案:

答案 0 :(得分:0)

更改此行

var oJSON_DataTable_DataHolder = document.getElementById("JSON_DataTable_DataHolder");

var oJSON_DataTable_DataHolder = document.getElementById("<%= JSON_DataTable_DataHolder.ClientID %>");

答案 1 :(得分:0)

您可以更改代码:

var oJSON_DataTable_DataHolder = document.getElementById("JSON_DataTable_DataHolder");

到:

var oJSON_DataTable_DataHolder = document.getElementById("ctl00_cphContent_JSON_DataTable_DataHolder");