iT邦幫忙

0

AJAX passing value from DropDownList 問題 (已解決)

  • 分享至 

  • xImage

最近在使用google visualization做報表分析
想實作使用DropDownList select的值撈DataBase的資料
但是遇上使用AJAX傳值的問題,資料都撈不出來
不知道大大們可以給點指教與解惑 @@
.aspx

<script type="text/javascript">
        google.load("visualization", "1", { packages: ["corechart"] });
        google.setOnLoadCallback(drawChart);
        function drawChart() {
            var options = {
                title: '使用量表',
                width: 1500,
                height: 1000,
                bar: { groupWidth: "90%" },
                legend: { position: "none" },
                isStacked: true
            };
            $.ajax({
                type: "POST",
                url: "Analysis_Page.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.BarChart($("#chart")[0]);
                    chart.draw(data, options);
                },
                failure: function (r) {
                    alert(r.d);
                },
                error: function (r) {
                    alert(r.d);
                }
            });
        }
        function ShowCurrentBuilding() {
        $.ajax(
                {
                    type: "POST",
                    url: "Analysis_Page.aspx/GetChartData",
                    data: '{PassValue: ' + $('#<%= parkDDL.ClientID %> option:selected').val() + '}',
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: OnSuccess,
                    failure: function (response) {
                    alert(response.d);
                    }
                });
        }
        function OnSuccess(response) {
        alert(response.d);
        }
</script>
<td>
<asp:DropDownList ID="parkDDL" ClientIDMode="Static" onchange="ShowCurrentBuilding()" style="width:150px;" runat="server" AutoPostBack="true">
   <asp:ListItem text="A" value="A"></asp:ListItem>
   <asp:ListItem text="B" value="B"></asp:ListItem>
   <asp:ListItem text="C" value="C"></asp:ListItem>
   <asp:ListItem text="D" value="D"></asp:ListItem>
</asp:DropDownList>
</td>

aspx.cs

[WebMethod]
    public static List<object> GetChartData(string PassValue)
    {
        string query = "SELECT name, COUNT(ID) Total_ID FROM Info INNER JOIN P_infor ON D_info.FK_Pa_infor_ID = P_infor.Pa_infor_ID WHERE [Level] = '" + PassValue + "' AND isnull(P_infor.D_Mark, '') = '' GROUP BY name ORDER BY Total_ID DESC";
        string constr = ConfigurationManager.ConnectionStrings["XX"].ConnectionString;
        List<object> chartData = new List<object>();
        chartData.Add(new object[]
        {
            "name", "Total_ID"
        });
        using (SqlConnection con = new SqlConnection(constr))
        {
            using (SqlCommand cmd = new SqlCommand(query))
            {
                cmd.CommandType = CommandType.Text;
                cmd.Connection = con;
                con.Open();
                using (SqlDataReader sdr = cmd.ExecuteReader())
                {
                    while (sdr.Read())
                    {
                        chartData.Add(new object[]
                        {
                            sdr["name"], sdr["Total_ID"] 
                        });
                    }
                }
                con.Close();
                return chartData;
            }
        }
    }
小火車 iT邦新手 4 級 ‧ 2018-12-27 14:57:50 檢舉
已找到解答 感謝各位
https://www.aspforums.net/Threads/394276/Load-Google-Chart-on-DropDownList-change-in-ASPNet-using-C-and-VBNet/
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友回答

立即登入回答