我在谷歌上搜索了几天,仍然找不到解决方案。我创建了3个ASP下拉列表,在用户选择字符串后,将该字符串传递给VB.net中的SQL query的webmethod
有人能给我一些提示吗?
谢谢。
下面是代码:
function draw2CavitiesChart() {
var options = {
title: '2 Line VS Cavities',
width: 1700,
height: 700,
//bar: { groupWidth: "95%" },
//curveType: 'function',
//isStacked: true
pointSize: 8,
hAxis: { title: 'Date', format: 'M/d/yy' },
vAxis: { title: 'Total Cavities' },
//colors: ['blue'],
legend: { position: "bottom" }
};
var vs_SelectedLine1 = ddlSelectedLine1;
var vs_SelectedLine2 = ddlSelectedLine2;
var vs_SelectedLine3 = ddlSelectedLine3;
$.ajax({
type: "POST",
url: "Chart.aspx/Get2CavitiesData",
date: { SelectedLine1: vs_SelectedLine1, SelectedLine2: vs_SelectedLine2, SelectedLine3: vs_SelectedLine3 },
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (r) {
var data = google.visualization.arrayToDataTable(r.d);
var chart = new google.visualization.LineChart($("#div2CavitiesChart")[0]);
chart.draw(data, options);
},
failure: function (r) {
alert(r.d);
},
error: function (r) {
alert(r.d);
}
});
}和代码背后: _公共函数Get2CavitiesDate()
Return (Me.ddlSelectedLine1.SelectedItem.ToString)
Return (Me.ddlSelectedLine2.SelectedItem.ToString)
Return (Me.ddlSelectedLine3.SelectedItem.ToString)
Dim constring As String = ConfigurationManager.ConnectionStrings("LocalDBConnectionString").ConnectionString
Dim chartData As New List(Of Object)()
chartData.Add(New Object() {"SelectedDate", "(SelectedLine1)", "(SelectedLine2)", "(SelectedLine3)"})
Using con As New SqlConnection(constring)
Using cmd As New SqlCommand()
cmd.CommandType = CommandType.StoredProcedure
cmd.CommandText = "ChartReportDataTable"
cmd.Connection = con
con.Open()
Using sdr As SqlDataReader = cmd.ExecuteReader()
While sdr.Read()
chartData.Add(New Object() {sdr("SelectedDate"), sdr("(SelectedLine1"), sdr("(SelectedLine2)"), sdr("(SelectedLine3)")})
End While
End Using
con.Close()
Return chartData
End Using
End Using
End Function和ASP:
DropDownList ID="ddlSelectedLine1“runat="server”AutoPostBack="false“/>
发布于 2020-10-30 21:13:35
您使用的是<asp:DropDownList>,因此在客户端JS中访问它的方法是使用<%= ddlSelectedLine1.ClientID %>。所以像var vs_SelectedLine1 = document.getElementById('<%= ddlSelectedLine1.ClientID %>).value这样的东西应该可以做到这一点。
此外,您将无法在代码隐藏中访问控件的值,因为任何正常的页面周期都不会执行,因此控件将具有从视图状态加载的值。
在JS客户端ajax调用中,您有一个输入错误的代码:
data: "{ 'SelectedLine1': '" + vs_SelectedLine1 + "', 'SelectedLine2': '" + vs_SelectedLine2 + "', 'SelectedLine3': '" + vs_SelectedLine3 + "' }"
您的后端代码签名将如下所示
public static <chartDataType> Get2CatvitiesDate(string SelectedLine1, string SelectedLine2, string SelectedLine3) { ... <rest of your function > }
我不知道你的后端方法的返回类型,但这应该可以让你上手。
https://stackoverflow.com/questions/64598625
复制相似问题