获取asp标签值并放入javascript函数中

Get asp label value and put in javascript function

本文关键字:javascript 函数 asp 标签 获取      更新时间:2023-09-26

我有两个标签,我想获得值并存储在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

你需要确保

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

被调用时,你只需要解析为int

使用

Convert.ToInt32(Label1.Text)

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