如何使用SqlDataSource选择级联DropDownList

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

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

在我选择’dog’的那一刻,显示了数据库中的所有品种。

   <asp:SqlDataSource ID="Species" runat="server" ConnectionString="" SelectCommand="SELECT DISTINCT [Species] FROM [Breed]">    <asp:SqlDataSource ID="breed" runat="server" ConnectionString="" SelectCommand="SELECT DISTINCT [Breed] FROM [Breed]">  

您需要在SelectParameters中使用ControlParameter

确保DropDownListSpecies的 AutoPostBack =“True”

仅供参考:您的Speecies错字

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

           

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

         

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