【问题标题】:Get asp label value and put in javascript function获取asp标签值并放入javascript函数
【发布时间】:2017-01-08 20:19:13
【问题描述】:

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

【问题讨论】:

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


    【解决方案1】:

    你需要确定

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

    被调用然后你只需要解析到 int

    使用

    Convert.ToInt32(Label1.Text)
    

    【讨论】:

      【解决方案2】:

      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
      

      【讨论】:

      • 感谢您的回答,我会试一试。但我希望这两个标签分开存储,就像这样。 { y: label1val } { y: label2val }
      • 马达利亚。尝试了您的解决方案,但它不起作用。我要做的就是获取标签值并存储在饼图标签中。你有什么想法让它发挥作用吗?谢谢。
      • 将标签文本存储在数组中并将其传递给changepie函数。您可以通过索引从数组中提取值并将其存储在相应的标签变量中。这就是我所做的。你得到了什么错误?
      • 他们没有错误。是不是只有另一个值没有显示。只有一个值。当用户单击 gridview 行时,我实际上呈现了标签值。
      • 编辑了我上面的代码,以便您可以看到我想要实现的目标。谢谢。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-19
      • 1970-01-01
      • 2019-12-30
      • 2011-03-14
      相关资源
      最近更新 更多