我正在创建一个包含2个下拉列表的网站,一个连接到显示所有产品名称的数据源,第二个显示所有客户的公司名称(northwind)。
选择了两个选项后,会出现一个按钮,显示一个gridview,显示orderid,orderdate,shipdate和quantity,如果有来自该客户的订单(dropdownlist2),他们订购了该产品(dropdownlist1),否则我如果没有发生这种情况,将设置验证器。
我不明白的是如何让第3个数据源的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
答案 0 :(得分:1)
请遵循此示例,假设ddlTwo
依赖于ddlOne
而GridView gv
依赖于DropDownLists的选择:
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
另外,如用户2615302所述,将AutoPostBack选项设置为True
答案 1 :(得分:0)
设置你的DropDownList1 AutoPostBack =“True” 我的猜测他们没有价值
尝试在Sql查询中调试wuth断点并查看查询的值是什么