如何使用依赖于ASP.NET中的互相多个数据源? [英] How to use multiple datasources that rely on each other in ASP.NET?

查看:126
本文介绍了如何使用依赖于ASP.NET中的互相多个数据源?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用2下拉列表一个网站,一个是连接到显示所有产品的名称的数据源,第二个显示所有客户(罗斯文)的公司名称。

I'm making a website with 2 dropdown lists, one is connected to a datasource that displays the names of all products, the second displays the company name of all customers (northwind).

之后,一个按钮被击中如果从客户(dropdownlist2)的订单,其显示的GridView,显示订单id,订购日期,SHIPDATE和数量,他们订购该产品(dropdownlist1),否则我将有一个验证器设置,如果这种情况并没有发生。

After both options are selected, a button is hit which displays a gridview that shows the orderid, orderdate, shipdate, and quantity if there is an order from that customer (dropdownlist2) where they ordered that product (dropdownlist1), otherwise I'll have a validator set up if that scenario hasn't happened.

我不明白的是如何取得第三个数据源的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


很抱歉,如果我要这一切错了,这是一个使用计算器我第一次


Sorry if I'm going about all this wrong, this is my first time using StackOverflow

推荐答案

按照这个例子中,假设 ddlTwo 取决于 ddlOne 和GridView GV 取决于选择两个DropDownLists的:

Follow this example, assuming that ddlTwo is dependant on ddlOne and GridView gv is dependant on selection of both 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

此外,作为user2615302提到的,设置的AutoPostBack选项设置为True

Also, as user2615302 mentioned, set the AutoPostBack option to True

这篇关于如何使用依赖于ASP.NET中的互相多个数据源?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆