簡體   English   中英

如何使用SqlDataSource選擇級聯的DropDownList

[英]How to select cascading DropDownList with SqlDataSource

我的數據庫中有以下內容-

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犬,比特犬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>

您需要在SelectParameters中使用ControlParameter

確保DropDownListSpecies的 AutoPostBack =“ True”

僅供參考:您在Speecies中有錯字

在此處輸入圖片說明在此處輸入圖片說明

<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>

您不會根據第一個下拉列表中的選擇(這就是您想要的)來過濾第二個下拉列表中的數據。

    <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的值后立即反映出更改,則需要向每個DropDownList添加AutoPostBack =“ True”屬性。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM