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

C# 根据其他下拉列表c中的值显示值的下拉列表,c#,mysql,asp.net,drop-down-menu,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_c

我有两个下拉列表,第一个显示不同种类的动物,第二个根据选择的种类显示品种

在我的数据库中,一个物种的所有品种都有一个匹配的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();
    }
第二个下拉列表的代码:

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

有人能告诉我问题在哪里和/或如何让这个想法起作用吗

您正在使用string.Format,但没有使用其中的参数

MySqlCommand cd = new MySqlCommand(string.Format(
         "SELECT * FROM (Breed.breed)", petSpecies.Text), cs);
您不应该使用如下字符串格式:

 var use_it_properly = string.Format ("Hi {0}, now you're using it properly", name);
否则,您就不会依赖于输入,而在您的情况下,输入应该是选定的物种

请阅读有关MSDN的更多信息:

该站点的示例:

DateTime dat = new DateTime(2012, 1, 17, 9, 30, 0); 
string city = "Chicago";
int temp = -16;
string output = String.Format("At {0} in {1}, the temperature was {2} degrees.",
                              dat, city, temp);
Console.WriteLine(output);
// The example displays the following output: 
//    At 1/17/2012 9:30:00 AM in Chicago, the temperature was -16 degrees. 

我不熟悉MySQL。下面是使用SqlDataSource可以做的事情

数据库表名为 截图

ASPX
请确保AutoPostBack=True用于DropDownlowstSpecies

第二个下拉列表-我认为问题出在您的数据库调用中。你能在调用后设置一个断点,看看有什么数据通过吗?我试过了,但问题是一开始什么都没有通过。我想我只需要写一个全新的数据库调用,但我不确定如何做。你必须使用for循环并获取列表中的每个项目。可以这样做:好的,但我不完全确定如何做,你是否可以创建一个示例?我对这方面非常陌生,所以我不完全确定,但我会尝试并实现它。谢谢,因为您希望第二个的输入取决于第一个,所以您需要根据所选值从数据库中选择一些内容。您可以使用字符串格式,但您必须在其中输入所需的部分。感谢您的反馈,我在阅读此内容时有点困难,您是否可以显示C代码?没有C代码,因为两个DropDownList都使用SqlDataSource。