2

我正在制作一个带有 2 个下拉列表的网站,一个连接到显示所有产品名称的数据源,第二个显示所有客户的公司名称(北风)。

选择这两个选项后,会点击一个按钮,显示一个显示 orderid、orderdate、shipdate 和数量的 gridview如果没有发生这种情况,则设置验证器。

我不明白的是如何让第三个数据源的 SQL 成为动态的,因为它需要使用 2 个下拉列表中的内容作为条件。到目前为止,这就是我所拥有的:

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
    GridView1.Visible = True

    Dim v1, v2 As String
    v1 = DropDownList1.SelectedItem.Value.ToString
    v2 = DropDownList2.SelectedItem.Value.ToString

    SqlDataSource3.SelectCommand = "select O.OrderID, OrderDate, ShippedDate, Quantity" _
                                 & "from Orders O, [Order Details] D, Customers C, Products P" _
                                 & "where O.OrderID = D.OrderID" _
                                 & "and C.CustomerID = O.CustomerID" _
                                 & "and P.ProductID = D.ProductID" _
                                 & "and companyname = '" & v1 & "'" _
                                 & "and productname = '" & v2 & "'"

End Sub

对不起,如果我做错了,这是我第一次使用 StackOverflow

4

2 回答 2

1

按照这个例子,假设GridView 和DropDownLists 的选择都ddlTwo依赖和依赖:ddlOnegv

Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
    If Not IsPostBack Then
        ddlOne.DataBind()
        ddlTwo.DataBind()
    End If
End Sub

Protected Sub ddlOne_DataBinding(sender As Object, e As EventArgs) Handles ddlOne.DataBinding
    sender.DataSource = someListOne
    sender.DataTextField = "TXT"
    sender.DataValueField = "ID"
End Sub

Protected Sub ddlTwo_DataBinding(sender As Object, e As EventArgs) Handles ddlTwo.DataBinding
    sender.DataSource = someListTwo
    sender.DataTextField = "TXT"
    sender.DataValueField = "ID"
End Sub

Protected Sub ddlOne_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlOne.SelectedIndexChanged
    ddlTwo.DataBind()
    gv.DataBind()
End Sub

Protected Sub ddlTwo_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlTwo.SelectedIndexChanged
    gv.DataBind()
End Sub

Protected Sub gv_DataBinding(sender As Object, e As EventArgs) Handles gv.DataBinding
    sender.DataKeyNames = {"ID"}
    sender.DataSource = GetDataSource(ddlOne.SelectedValue, ddlTwo.SelectedValue)
End Sub

此外,正如 user2615302 提到的,将 AutoPostBack 选项设置为 True

于 2013-10-22T21:39:48.317 回答
0

设置你的 DropDownList1 AutoPostBack="True" 我猜他们在那里没有价值

尝试在 Sql 查询处调试断点并查看查询的值是什么

于 2013-10-22T21:24:53.690 回答