无法将参数传递给Google饼图MySql查询

时间:2018-10-17 10:48:41

标签: javascript c# mysql asp.net webmethod

我有这个asp标签,它从会话中获取其值。我想将其作为参数传递给WebMethod中用于Mysql饼图的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中的查询。我怎样才能做到这一点。预先感谢。

1 个答案:

答案 0 :(得分:0)

1)向您的string中添加WebMethod参数

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

2)像这样从js发送string参数到上述方法中

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
});