2

我的数据库中有以下内容 -

breedId Species Breed
0       dog      Alsatian
1       dog      pitbull
2       dog      Shetland sheepdog
3       dog      Boxer
4       cat      Dragon Li
5       cat      Australian Mist
6       cat      Korat

在 c# 设计器视图中,我有 2 个下拉列表,其中一个包含物种,另一个是品种。

我想要的是当用户在物种列表中选择“狗”时,品种列表应该有以下内容Alsatian, pitbull, Shetland sheepdog,Boxer

在我选择“狗”的那一刻,数据库中的所有品种都会显示出来。

<asp:DropDownList ID="DropDownListSpecies" runat="server" 
     Height="27px" Width="107px" DataSourceID="hs330" 
     DataTextField="Species" DataValueField="Species">
</asp:DropDownList>
<asp:SqlDataSource ID="Species" runat="server" 
     ConnectionString="<%$ ConnectionStrings:ConnectionString %>" 
     SelectCommand="SELECT DISTINCT [Species] FROM [Breed]">
</asp:SqlDataSource>

<asp:DropDownList ID="DropDownListBreed" runat="server" Height="20px" 
    Width="110px" DataSourceID="breed" DataTextField="Breed" 
    DataValueField="Breed">
</asp:DropDownList>
<asp:SqlDataSource ID="breed" runat="server" 
    ConnectionString="<%$ ConnectionStrings:ConnectionString %>" 
    SelectCommand="SELECT DISTINCT [Breed] FROM [Breed]">
</asp:SqlDataSource>
4

2 回答 2

3

您需要在SelectParameters中使用ControlParameter

确保DropDownListSpeciesAutoPostBack="True"

仅供参考:你在物种中有错字

在此处输入图像描述 在此处输入图像描述

<asp:DropDownList ID="DropDownListSpecies" runat="server"
    Height="27px" Width="107px" DataSourceID="Species"
    DataTextField="Species" DataValueField="Species" AutoPostBack="True">
</asp:DropDownList>
<asp:SqlDataSource ID="Species" runat="server"
    ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
    SelectCommand="SELECT DISTINCT [Species] FROM [Breed]"></asp:SqlDataSource>
<asp:DropDownList ID="DropDownListBreed" runat="server"
    Height="20px" Width="110px"
    DataSourceID="breed" DataTextField="Breed" DataValueField="Breed">
</asp:DropDownList>
<asp:SqlDataSource ID="breed" runat="server"
    ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
    SelectCommand="SELECT DISTINCT [Breed] FROM [Breed] WHERE Species=@Species">
    <SelectParameters>
        <asp:ControlParameter ControlID="DropDownListSpecies" PropertyName="SelectedValue"
            Name="Species " Type="String" DefaultValue="cat" />
    </SelectParameters>
</asp:SqlDataSource>
于 2013-11-06T23:17:30.297 回答
1

您没有根据在第一个下拉列表中所做的选择(这是您想要的)过滤第二个下拉列表中的数据。

    <asp:DropDownList ID="DropDownListBreed" runat="server" Height="20px" Width="110px" DataSourceID="breed" DataTextField="Breed" DataValueField="Breed">
    </asp:DropDownList>
    <asp:SqlDataSource ID="breed" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" SelectCommand="SELECT DISTINCT [Breed] FROM [Breed] WHERE Species = @Species">
    <SelectParameters>
        <asp:ControlParameter ControlID="DropDownListSpecies" PropertyName="SelectedValue"
            Name="Species " Type="String" DefaultValue="cat" />
    </SelectParameters>
</asp:SqlDataSource>

此外,如果您希望在更改每个 DropDownList 的值后立即反映更改,则需要将AutoPostBack="True"属性添加到每个 DropDownList。

于 2013-11-06T23:13:07.717 回答