gpt4 book ai didi

c# - 如何选择级联的DropDownList与SqlDataSource

转载 作者:行者123 更新时间:2023-11-30 20:53:38 28 4
gpt4 key购买 nike

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

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# 设计器 View 中,我有 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>

最佳答案

您需要在SelectParameters中使用ControlParameter

确保 AutoPostBack="True" 用于 DropDownListSpecies

仅供引用:您在物种

中有错字

enter image description here enter image description here

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

关于c# - 如何选择级联的DropDownList与SqlDataSource,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19824540/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com