You need to enable JavaScript to run this app. VB.NET实现ASP DropDownList选中值传递给WebMethod执行SQL查询
优惠活动
大模型
产品
解决方案
定价
更多

VB.NET实现ASP DropDownList选中值传递给WebMethod执行SQL查询

现有代码错误排查
  • 前端逻辑错误:
    • 直接使用服务器端DropDownList的ID赋值,无法获取渲染后页面的DOM元素,ASP.NET会给服务器控件生成带命名容器的客户端ID,不能直接访问
    • ajax配置项拼写错误:date应为data
    • 未将请求参数序列化为JSON格式,后端WebMethod无法解析接收
    • 按钮触发客户端事件后未阻止默认回发,会导致页面刷新中断ajax请求
  • 后端WebMethod错误:
    • 未声明为Shared静态方法,不符合ASP.NET WebMethod的调用要求
    • 方法名和ajax请求的路径不匹配(前端请求Get2CavitiesData,后端定义为Get2CavitiesDate)
    • 存在多个冗余return语句,首个return执行后后续所有代码(包括SQL查询逻辑)都不会运行
    • 静态方法无页面实例上下文,无法直接访问Me.ddlSelectedLine1这类服务器控件,必须通过前端传参获取选中值
    • 存储过程未传入参数,无法根据选中的产线过滤数据,且数据列名拼写混乱、多余括号会导致读取报错
  • 冗余控件问题:页面中额外添加的3个TextBox控件无实际作用,可直接删除
修正后实现代码

Aspx 页面代码

<asp:DropDownList ID="ddlSelectedLine1" runat="server" AutoPostBack="false" />
<asp:DropDownList ID="ddlSelectedLine2" runat="server" AutoPostBack="false" />
<asp:DropDownList ID="ddlSelectedLine3" runat="server" AutoPostBack="false" />
<%-- 增加return false阻止按钮默认回发 --%>
<asp:Button ID="btnGenChart1" runat="server" Text="Plot Cavities" Width="100px" OnClientClick="draw2CavitiesChart(); return false;" />

前端 JS 代码

function draw2CavitiesChart() {
    var options = {
        title: '2 Line VS Cavities',
        width: 1700,
        height: 700,
        pointSize: 8,
        hAxis: { title: 'Date', format: 'M/d/yy' },
        vAxis: { title: 'Total Cavities' },
        legend: { position: "bottom" }
    };
    // 通过ClientID获取渲染后的真实DOM ID,读取选中值
    var vs_SelectedLine1 = $('#<%= ddlSelectedLine1.ClientID %>').val();
    var vs_SelectedLine2 = $('#<%= ddlSelectedLine2.ClientID %>').val();
    var vs_SelectedLine3 = $('#<%= ddlSelectedLine3.ClientID %>').val();
    
    $.ajax({
        type: "POST",
        url: "Chart.aspx/Get2CavitiesData",         
        // 修正拼写为data,序列化为JSON字符串
        data: JSON.stringify({
            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);
        },
        error: function (r) {
            // 读取实际错误信息调试
            alert(r.responseText);
        }
    });
}

后端 VB.NET WebMethod 代码

' WebMethod必须为Shared,添加ScriptMethod特性支持POST请求
<WebMethod()>
<ScriptMethod(ResponseFormat:=ResponseFormat.Json)>
Public Shared Function Get2CavitiesData(SelectedLine1 As String, SelectedLine2 As String, SelectedLine3 As String) As List(Of Object)
    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
            ' 给存储过程传入3个产线过滤参数,参数名需和存储过程定义一致
            cmd.Parameters.AddWithValue("@TargetLine1", SelectedLine1)
            cmd.Parameters.AddWithValue("@TargetLine2", SelectedLine2)
            cmd.Parameters.AddWithValue("@TargetLine3", SelectedLine3)
            con.Open()
            
            Using sdr As SqlDataReader = cmd.ExecuteReader()
                While sdr.Read()
                    ' 列名需和存储过程返回的数据集列名完全匹配
                    chartData.Add(New Object() {
                        sdr("SelectedDate"),
                        sdr("Line1Cavities"),
                        sdr("Line2Cavities"),
                        sdr("Line3Cavities")
                    })
                End While
            End Using
        End Using
    End Using
    
    Return chartData
End Function
额外配置说明
  • 存储过程ChartReportDataTable需提前定义3个输入参数,用于接收选中的产线名称,返回对应时间维度的 cavities 统计数据,返回的列名需和代码中sdr("列名")的取值完全一致
  • 若DropDownList的选项是后台绑定的,需确保在!IsPostBack分支中完成下拉框选项绑定,避免重复覆盖客户端选中值
  • 需在页面头部引入jQuery和Google Charts的依赖脚本,否则会出现方法未定义的报错

内容的提问来源于stack exchange,提问作者Andy Man

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.11.19 16:15:03