简体   繁体   English

dropdownlist 将参数传递给 vb.net 中的 webmethod

[英]dropdownlist pass parameter to webmethod in vb.net

I googled on this a couple days and still cannot figure out the solution.我用谷歌搜索了几天,仍然无法找出解决方案。 I created 3 ASP dropdownlist, after the user select the string, pass the string to webmethod for SQL query in VB.net我创建了 3 个 ASP 下拉列表,在用户选择字符串后,将字符串传递给 webmethod 以在 VB.net 中进行 SQL 查询

Can anyone give me some hint on this?任何人都可以给我一些提示吗?

Thank you.谢谢你。

herewith the code:特此代码:

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

and code behind: <WebMethod()> _ Public Function Get2CavitiesDate()和后面的代码: <WebMethod()> _ Public Function 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

and ASP:和 ASP:

<asp:DropDownList ID="ddlSelectedLine1" runat="server" AutoPostBack="false" /> <asp:DropDownList ID="ddlSelectedLine2" runat="server" AutoPostBack="false" /> <asp:DropDownList ID="ddlSelectedLine3" runat="server" AutoPostBack="false" /> <asp:Button ID="btnGenChart1" runat="server" Text="Plot Cavities" Width="100px" OnClientClick="draw2CavitiesChart()" /> <asp:TextBox ID="VS_SelectedLine1" runat="server" /> <asp:TextBox ID="VS_SelectedLine2" runat="server" /> <asp:TextBox ID="VS_SelectedLine3" runat="server" /> <asp:DropDownList ID="ddlSelectedLine1" runat="server" AutoPostBack="false" /> <asp:DropDownList ID="ddlSelectedLine2" runat="server" AutoPostBack="false" /> <asp:DropDownList ID="ddlSelectedLine3 " runat="server" AutoPostBack="false" /> <asp:Button ID="btnGenChart1" runat="server" Text="Plot Cavities" Width="100px" OnClientClick="draw2CavitiesChart()" /> <asp: TextBox ID="VS_SelectedLine1" runat="server" /> <asp:TextBox ID="VS_SelectedLine2" runat="server" /> <asp:TextBox ID="VS_SelectedLine3" runat="server" />

You are using the <asp:DropDownList> so the way to access this in the clientside JS is by using <%= ddlSelectedLine1.ClientID %> .您正在使用<asp:DropDownList>因此在客户端 JS 中访问它的方法是使用<%= ddlSelectedLine1.ClientID %> So something like var vs_SelectedLine1 = document.getElementById('<%= ddlSelectedLine1.ClientID %>).value should do the trick.所以像var vs_SelectedLine1 = document.getElementById('<%= ddlSelectedLine1.ClientID %>).value这样的东西应该可以解决问题。

Also you won't be able to Access the values of the controls in the code-behind as none of the normal page cycle will have executed such that the control will have the values loaded from the viewstate.此外,您将无法访问代码隐藏中控件的值,因为正常页面循环都不会执行,因此控件将从视图状态加载值。

In your JS client-side ajax call you have code you have a typo:在您的 JS 客户端 ajax 调用中,您有一个拼写错误的代码:

data: "{ 'SelectedLine1': '" + vs_SelectedLine1 + "', 'SelectedLine2': '" + vs_SelectedLine2 + "', 'SelectedLine3': '" + vs_SelectedLine3 + "' }"

And your back end code signature will look like你的后端代码签名看起来像

public static <chartDataType> Get2CatvitiesDate(string SelectedLine1, string SelectedLine2, string SelectedLine3) { ... <rest of your function > }

I don't know the return type of your backend method but this should get you going.我不知道您的后端方法的返回类型,但这应该可以帮助您前进。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM