asp:选择dropdownlist.SelectedItem作为SQL WHERE条件

时间:2016-01-31 21:35:30

标签: sql asp.net

如何在SqlDataSource中正确设置sql语句?

<asp:DropDownList ID="customersDropDownList" runat="server" DataSourceID="customerSqlDataSource" DataTextField="KLIENT" DataValueField="KLIENT">
<asp:ListItem>Klient</asp:ListItem>
</asp:DropDownList>


<asp:SqlDataSource ID="projektSqlDataSource" runat="server" 
ConnectionString="<%$ ConnectionStrings:pd1960ConnectionString %>" 

    SelectCommand="SELECT DISTINCT cast(PROJECT AS VARCHAR) + 
': ' + OPIS AS ProjektOPIS FROM [V_works] 
WHERE ([KLIENT]=" + customersDropDownList.SelectedItem.Text + ")">

    </asp:SqlDataSource>

1 个答案:

答案 0 :(得分:0)

尝试类似下面的内容

<asp:SqlDataSource ID="projektSqlDataSource" runat="server"
    ConnectionString="<%$ ConnectionStrings:pd1960ConnectionString %>"
    SelectCommand="SELECT DISTINCT cast(PROJECT AS VARCHAR) + 
': ' + OPIS AS ProjektOPIS FROM [V_works] 
WHERE ([KLIENT]=@Customer">
    <SelectParameters>
        <asp:ControlParameter ControlID="customersDropDownList" PropertyName="SelectedValue"
            Name="Customer" Type="String" DefaultValue="Klient" />
    </SelectParameters>
</asp:SqlDataSource>

并确保在下拉控件中设置AutoPostBack = "True"