c# - 根据其他下拉 C# 中的值下拉显示值

标签 c# mysql asp.net drop-down-menu

我有两个下拉列表,第一个显示不同种类的动物,我希望第二个显示品种取决于选择的物种。

在我的数据库中,一个物种的所有品种都有匹配的 ID。

第一个下拉代码:

protected void Page_Load(object sender, EventArgs e)
{
    DropDown_Species();
}

public void DropDown_Species()
{
    if (!Page.IsPostBack)
    {

        MySqlCommand sql_country = new MySqlCommand("SELECT Species FROM breed", cs);
        //connection string opend
        cs.Open();

        MySqlDataReader ddlvalue;
        ddlvalue = sql_country.ExecuteReader();

        petSpecies.DataSource = ddlvalue;
        petSpecies.DataValueField = "Species";
        petSpecies.DataTextField = "Species";
        petSpecies.DataBind();
        petSpecies.Items.Insert(0, "Select Species");
        // connection string closed
        cs.Close();
        cs.Dispose();
    }
}

第二个下拉代码:

protected void petsBreed_SelectedIndexChanged(object sender, EventArgs e)
 {
    if (petSpecies.Text != string.Empty)
    {
        MySqlCommand cd = new MySqlCommand(string.Format(
             "SELECT * FROM (Breed.breed)", petSpecies.Text), cs);
        cs.Open();
        MySqlDataReader petsSpecies = cd.ExecuteReader();
        petsBreed.DataSource = petsSpecies;
        petsBreed.DataValueField = "Breed";
        petsBreed.DataTextField = "Breed";
        petsBreed.DataBind();
        petsBreed.Items.Insert(0, "Select Breed");
        cs.Close();
        cs.Dispose();
    }

我很确定问题出在第二个下拉列表中。

对 C# 非常陌生。

谁能告诉我问题出在哪里和/或如何使这个想法奏效?

最佳答案

我不熟悉 MySQL。以下是您可以使用 SqlDataSource 执行的操作。

数据库(表名为Breed)

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

截图

enter image description here enter image description here

ASPX

<asp:DropDownList ID="DropDownListSpecies" runat="server" 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"
   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>

确保 AutoPostBack="True" 用于 DropDownListSpecies

关于c# - 根据其他下拉 C# 中的值下拉显示值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20447654/

相关文章:

.net - 自定义 GridView 删除按钮

c# - WCF 数据服务 : How to query ALL entities when Paging is enabled?

c# - 当我使用非共享运行时构建 Monodroid 项目时获取编码失败

mysql - 返回产品和产品依赖关系的 SQL

mysql - 用于电子商务 SaaS 的 Postgres 共享架构 Multi-Tenancy 设置

php - 如何使用SQL插入不存在的东西,如果存在则删除它?

javascript - 如何从后面的代码传递 JavaScript 函数中的逗号分隔字符串?

c# - RegEx.Replace 但排除 html 标签内的匹配项?

c# - 为什么JScript中的同名变量和函数会发生冲突?

c# - 如何在 Linq 中编写 SQL Concat?