【问题标题】:Use a selected item from a dropdownlist in a SELECT query在 SELECT 查询中使用下拉列表中的选定项目
【发布时间】:2017-10-12 22:35:24
【问题描述】:

我有以下代码,它是航班位置的下拉列表:

Where to:<br />
    <asp:DropDownList ID="DropDownList1" runat="server"
        DataSourceID="SqlDataSource1" 
        DataTextField="Destination" 
        DataValueField="Destination" Width="222px">
    </asp:DropDownList>
    <asp:SqlDataSource ID="SqlDataSource1" runat="server"
        ConnectionString="<%$ ConnectionStrings:ConnectionString %>" 
        SelectCommand="SELECT [Destination] FROM [tblFlight]">
    </asp:SqlDataSource>

我希望能够使用从上面的下拉列表中选择的值(其中一个是科孚岛)作为查询的 WHERE 条件,该查询将只选择前往科孚岛的航班。 有什么建议吗?

【问题讨论】:

    标签: sql asp.net select drop-down-menu


    【解决方案1】:

    基本上:

    string dest = yourDropDown.SelectedItem; SELECT [Destination] FROM [tblFlight] WHERE [Destination] = " + dest;

    【讨论】:

    • 我意识到我在错误的地方有 WHERE 条件。在显示信息的地方,我有以下代码:&lt;asp:SqlDataSource ID="SqlDataSource3" runat="server" ConnectionString="&lt;%$ ConnectionStrings:ConnectionString %&gt;" SelectCommand="SELECT tblAirport.Name, tblFlight.Destination, tblFlight.FlightDate FROM tblAirport INNER JOIN tblFlight ON tblAirport.AirportCode = tblFlight.AirportCode WHERE Destination = DropDownList1.SelectedItem.Value"&gt;&lt;/asp:SqlDataSource&gt; 但从这里我得到错误说“DropDownList1.SelectedItem.Value”无法绑定。想法?
    • 尝试在 WHERE 子句中指定表:WHERE tblFlight.Destination = DropDownList1.SelectedItem.Value。不知道这是否足够。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-08
    • 2015-08-15
    • 2019-07-29
    • 2017-06-15
    相关资源
    最近更新 更多