获取asp标签值并输入javascript函数

时间:2016-09-01 05:20:48

标签: javascript jquery asp.net vb.net pie-chart

我有两个标签,我想获取值并存储在javascript函数中。 我希望将值放在饼图标签中。我只得到一个,我不知道如何在另一个标签。请帮助我。

这是我到目前为止所拥有的。

** Javascript **

    var pie = 0;
    function changepie(val) {
        pie = val;
    }
    var pie2 = 0;
    function changepie2(val) {
          pie2 = val;
      }

前端

 <asp:Label ID="Label1" runat="server" Text="Label" Visible="false"?</asp:Label>
 <asp:Label ID="Label2" runat="server" Text="Label" Visible="true"></asp:Label>

饼图加载的另一个javascript

 window.onload = function () {
            var chart = new CanvasJS.Chart("chartContainer",
            {
                animationEnabled: true,
                animationDuration: 1300,
                backgroundColor: "transparent",
                legend: {
                    verticalAlign: "bottom",
                    horizontalAlign: "center"
                },
                data: [
                {

                    indexLabelFontSize: 10,
                    indexLabelFontWeight: "bold",
                    indexLabelFontFamily: "Helvetica",
                    indexLabelPlacement: "outside",
                    indexLabelLine: "none",
                    radius: "100%",
                    type: "pie",

                    toolTipContent: "{y} - <strong>#percent%</strong>",
                    dataPoints: [
                        { y: pie, legendText: "", 
                                    label: pie + "%", 
                                    indexLabelLineColor: "#1dc7ea", 
                                    indexLabelFontColor: "#1dc7ea", 
                                    exploded: true, 
                                    indexLabelPadding: "5px"
                                    },
                        { y: pie2, legendText: "",
                                    label: pie2, 
                                    indexLabelLineColor: "#FF4A55", 
                                    indexLabelFontColor: "#FF4A55" 
                                    }

                    ]
                }
                ]
            });
            chart.render();
        }

,这是我的后端

Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load

    Label1.Text = Session("percent").ToString.Replace("<span class>", "").Replace("</span>", "")
    Label2.Text = Session("aaa").ToString
    ClientScript.RegisterClientScriptBlock(Me.[GetType](), "Script", "changepie(" + Label1.Text + ");", True)
    ClientScript.RegisterClientScriptBlock(Me.[GetType](), "Script", "changepie2(" + Label2.Text + ");", True)
End Sub

2 个答案:

答案 0 :(得分:0)

changepie 函数中的

使用此

    var label1= 0;
        function changepie(lbael1,label2) {

             var chart = new CanvasJS.Chart("chartContainer",
            {
                animationEnabled: true,
                animationDuration: 1300,
                backgroundColor: "transparent",
                legend: {
                    verticalAlign: "bottom",
                    horizontalAlign: "center"
                },
                data: [
                {

                    indexLabelFontSize: 10,
                    indexLabelFontWeight: "bold",
                    indexLabelFontFamily: "Helvetica",
                    indexLabelPlacement: "outside",
                    indexLabelLine: "none",
                    radius: "100%",
                    type: "pie",

                    toolTipContent: "{y} - <strong>#percent%</strong>",
                    dataPoints: [
                        { y: lbael1+", ", legendText: "", 
                                    label: pie + "%", 
                                    indexLabelLineColor: "#1dc7ea", 
                                    indexLabelFontColor: "#1dc7ea", 
                                    exploded: true, 
                                    indexLabelPadding: "5px"
                                    },
                        { y: label2, legendText: "",
                                    label: pie2, 
                                    indexLabelLineColor: "#FF4A55", 
                                    indexLabelFontColor: "#FF4A55" 
                                    }

                    ]
                }
                ]
        }


----------
Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load

    Label1.Text = Session("percent").ToString.Replace("<span class>", "").Replace("</span>", "")
    Label2.Text = Session("aaa").ToString
    ClientScript.RegisterClientScriptBlock(Me.[GetType](), "Script", "changepie(" + Label1.Text + "," + Label2.Text + ");", True)

End Sub

答案 1 :(得分:0)

You ned to make sure if

RegisterClientScriptBlock(Me.[GetType](), "Script", "changepie(" + Label1.Text + ");", True)

is getting called then you simply need to parseto int

using

Convert.ToInt32(Label1.Text)