【问题标题】:Can't pass parameter to google pie chart MySql query无法将参数传递给谷歌饼图 MySql 查询
【发布时间】:2018-10-17 10:48:41
【问题描述】:

我有这个从会话中获取值的 asp 标签。我想将它作为参数传递给 WebMethod 中的 MySql 查询,该查询用于此处的谷歌饼图。我尝试了很多,每次标签在 web 方法中返回 null。

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript">
    google.load("visualization", "1", { packages: ["corechart"] });
    google.setOnLoadCallback(drawChart);
    function drawChart() {
        var options = {
            title: 'Shift based data',
            legend: { position: 'right', textStyle: { color: 'blue', fontSize: 16 } }
        };
        $.ajax({
            type: "POST",
            url: "grpm.aspx/GetChartData",
            data: '{}',
            contentType: "application/json; charset=utf-8",
            dataType: "json",

            success: function (r) {
                var data = google.visualization.arrayToDataTable(r.d);
                var chart = new google.visualization.PieChart($("#chart")[0]);
                chart.draw(data, options);
            },
            failure: function (r) {
                alert(r.d);
            },
            error: function (r) {
                alert(r.d);
            }
        });
    }
</script>
<asp:Label ID="uName" runat="server"></asp:Label>

下面的静态方法

[System.Web.Services.WebMethod]
    public static List<object> GetChartData()
    {
        string query = "SELECT tm AS TM, COUNT(agentlogin)AS totalApproved, shift AS Shift FROM approved WHERE grpM = @grpm GROUP BY shift";
        string MyConString = ConfigurationManager.ConnectionStrings["connStr"].ConnectionString;
        List<object> chartData = new List<object>();
        chartData.Add(new object[]
        {
            "Shift", "totalApproved"
        });
        using (MySqlConnection con = new MySqlConnection(MyConString))
        {
            using (MySqlCommand cmd = new MySqlCommand(query))
            {

                cmd.Connection = con;
                Page page = (Page)HttpContext.Current.Handler as Page;
                Label login = (Label)page.FindControl("uName") as Label;
                cmd.CommandType = CommandType.Text;
                cmd.Parameters.AddWithValue("@grpm", login);
                con.Open();
                using (MySqlDataReader sdr = cmd.ExecuteReader())
                {
                    while (sdr.Read())
                    {
                        chartData.Add(new object[]
                        {
                            sdr["Shift"], sdr["totalApproved"]
                        });
                    }
                }
                con.Close();
                return chartData;
            }
        }
    }

我听说有一种方法可以通过 jquery 获取控件。如果有我如何将它传递给 WebMethod 中的查询。我怎样才能做到这一点。提前致谢。

【问题讨论】:

  • 可以像data: "{'paramName:" + value + "'}",一样传递data中的参数
  • 这似乎是一种奇怪的方法。服务器不应该知道自己的会话吗?我认为您不必在客户端 HTML 文档中呈现它,然后将其传回。

标签: javascript c# mysql asp.net webmethod


【解决方案1】:

1) 将string 参数添加到您的WebMethod

[WebMethod]
public static List<object> GetChartData(string name)
{
   //Your stuff here
}

2) 并将string 参数从js 发送到上述方法

var obj = {};
obj.name = 'Your value here';

$.ajax({
    type: "POST",
    url: "grpm.aspx/GetChartData",
    data: JSON.stringify(obj),
    contentType: "application/json; charset=utf-8",
    dataType: "json",

    //Your stuff here
});

【讨论】:

  • 它只传递 name="" 它没有从标签中获取它。
  • @prkash,将您的标签值传递给obj.name = document.getElementById('&lt;%=myID.ClientID%&gt;').innerHTML;
猜你喜欢
  • 1970-01-01
  • 2017-04-19
  • 2016-07-06
  • 2015-03-06
  • 1970-01-01
  • 2018-05-11
  • 1970-01-01
  • 2017-09-27
  • 2014-08-23
相关资源
最近更新 更多