javascript ASP.NET hiddenfield从代码背后获取价值

时间:2012-11-18 16:47:23

标签: c# javascript asp.net hiddenfield

我正在尝试将我的代码集成到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时,它变为null。

4 个答案:

答案 0 :(得分:0)

您可以尝试以下方法:

var yAxis = document.getElementById('<%# txtYAxisName.ClientID %>');

答案 1 :(得分:0)

首先获取需要使用<%=txtYAxisName.ClientID%>的控件的呈现ID,并且代码上的内容将为:

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

但要实际获取它的值,您需要使用.value属性。

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

但是如果你实际上不需要改变,nether使用post back这个隐藏的字段你可以直接把代码编写为:

<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\";";

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

答案 2 :(得分:0)

var yAxis = document.getElementById('<%# txtYAxisName.ClientID %>');

答案 3 :(得分:-1)

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

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

然后我尝试下面的工作。感谢Cal279:)

var xAxis = document.getElementById("MainContent_txtXAxisName");