无法将参数传递给google饼图mysql查询

bwntbbo3  于 2021-06-20  发布在  Mysql
关注(0)|答案(1)|浏览(370)

我有一个asp标签,它从会话中获取它的值。我想把它作为参数传递给webmethod中的mysql查询,它用于google饼图。我试了很多次,每次标签在webmethod中返回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中的查询。我怎样才能完成这件事。提前谢谢。

3npbholx

3npbholx1#

1) 添加 string 参数到您的 WebMethod 喜欢

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

2) 并发送 string 从js-like到上述方法的参数

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

相关问题