如何选择与SqlDataSource的级联的DropDownList [英] How to select cascading DropDownList with SqlDataSource
问题描述
我在我的数据库如下 -
breedId种品种
0阿尔萨斯狗
1狗比特犬
2犬喜乐蒂牧羊犬
3狗义和团
4猫李龙
5猫澳大利亚雾
6呵叻猫
在C#的设计师认为,我有2个下拉列表中选择一个具有物种和其他的品种。
我要的是,:当用户在挑选物种名单狗,
该品种名单应具有以下阿尔萨斯,斗牛犬,喜乐蒂牧羊犬,拳师
目前,当我拿起狗,都可以通过数据库的品种所示。
< ASP:DropDownList的ID =DropDownListSpecies=服务器
HEIGHT =27pxWIDTH =107px的DataSourceID =hs330
DataTextField =种DataValueField =种>
< / ASP:DropDownList的>
< ASP:SqlDataSource的ID =种=服务器
的ConnectionString =下;%$的ConnectionStrings:的ConnectionString%>中
的SelectCommand =SELECT DISTINCT [种] FROM [品种]>
< / ASP:SqlDataSource的>< ASP:DropDownList的ID =DropDownListBreed=服务器HEIGHT =20像素
WIDTH =110px的DataSourceID =繁殖DataTextField =繁殖
DataValueField =品种>
< / ASP:DropDownList的>
< ASP:SqlDataSource的ID =繁殖=服务器
的ConnectionString =下;%$的ConnectionStrings:的ConnectionString%>中
的SelectCommand =SELECT DISTINCT [品种] FROM [品种]>
< / ASP:SqlDataSource的>
您需要先使用 ControlParameter SelectParameters
请确保该的AutoPostBack =真对于 DropDownListSpecies
供参考:您有错字的 Speecies
< ASP:DropDownList的ID =DropDownListSpecies=服务器
HEIGHT =27pxWIDTH =107px的DataSourceID =种
DataTextField =种DataValueField =种的AutoPostBack =真>
< / ASP:DropDownList的>
< ASP:SqlDataSource的ID =种=服务器
的ConnectionString =下;%$的ConnectionStrings:的ConnectionString%>中
的SelectCommand =SELECT DISTINCT [种] FROM [品种]>< / ASP:SqlDataSource的>
< ASP:DropDownList的ID =DropDownListBreed=服务器
HEIGHT =20像素WIDTH =110px
的DataSourceID =繁殖DataTextField =品种DataValueField =品种>
< / ASP:DropDownList的>
< ASP:SqlDataSource的ID =繁殖=服务器
的ConnectionString =下;%$的ConnectionStrings:的ConnectionString%>中
的SelectCommand =SELECT DISTINCT [品种] FROM [品种] WHERE种= @种>
< SelectParameters>
< ASP:ControlParameter控件ID =DropDownListSpecies属性名=的SelectedValue
NAME =种类型=字符串默认值=猫/>
< / SelectParameters>
< / ASP:SqlDataSource的>
I have the following in my data base -
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
In the c# designer view, I have 2 drop-down list one which has species and other for breed.
What I want is that when user picks 'dog' in species list,
the breed list should have the following Alsatian, pitbull, Shetland sheepdog,Boxer
At the moment when I pick 'dog', all the breed from the database is shown.
<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>
You need to use ControlParameter in SelectParameters.
Make sure that AutoPostBack="True" for DropDownListSpecies
FYI: You have typo in 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>
这篇关于如何选择与SqlDataSource的级联的DropDownList的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!