简体   繁体   English

从另一个下拉列表中选择一个值后填充下拉列表

[英]Populating dropdown list after selecting a value from another dropdown list

I want to be able to populate a dropdown list(ddlExercise) after selecting a value from another dropdown list(ddlType) At the moment I am getting the values for the ddlType dropdown from a sql query which is populating the dropdown list but when I select something, the 2nd dropdown list remains empty. 我希望能够在从另一个下拉列表中选择一个值后填充下拉列表(ddlExercise)(ddlType)目前我从sql查询中获取ddlType下拉列表的值,该查询填充了下拉列表但是当我选择时第二个下拉列表仍然是空的。 I have 3 values in the ddlType dropdown(Gym,Core,Cardio) and once I select one of these, I am using the dropdownlist.selectedvalue to retrieve its corresponding results. 我在ddlType下拉列表中有3个值(Gym,Core,Cardio),一旦我选择其中一个,我使用dropdownlist.selectedvalue来检索其相应的结果。 Here is my code: 这是我的代码:

    protected void Page_Load(object sender, EventArgs e)
    {
       SqlConnection con = new SqlConnection(@"Con String");


        if (!IsPostBack)

        { BindExerciseType(); }
    }

     public void BindExerciseType()
    {
        con.Open();
        SqlCommand cmd = new SqlCommand("select ExerciseType from ExerciseType", con);
        SqlDataReader dr = cmd.ExecuteReader();
        ddlType.DataSource = dr;
        ddlType.Items.Clear();
        ddlType.Items.Add("--Please Select country--");
        ddlType.DataTextField = "ExerciseType";
        ddlType.DataValueField = "ExerciseType";
        ddlType.DataBind();
        con.Close();



    }

    public void BindExercise()
    {
        con.Open();
        SqlCommand cmd = new SqlCommand("select ExerciseName from ExerciseDisplay   Where TypeName='" + ddlType.SelectedValue + "'", con);
        SqlDataReader dr = cmd.ExecuteReader();
        ddlExercise.DataSource = dr;
        ddlExercise.Items.Clear();
        ddlExercise.Items.Add("--Please Select country--");
        ddlExercise.DataTextField = "ExerciseName";
        ddlExercise.DataValueField = "ExerciseName";
        ddlExercise.DataBind();
        con.Close();

    }

        protected void ddlExercise_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindExercise();
    }

Any ideas why this may be wrong? 任何想法为什么这可能是错的? I have the autopostback propert set to true on both drop down lists. 我在两个下拉列表中都将autopostback属性设置为true。 Any help would be greatly appreciated! 任何帮助将不胜感激!

Your second SQL query is wrong. 你的第二个SQL查询是错误的。 There should be a space between WHERE and TypeName. WHERE和TypeName之间应该有一个空格。 Like This : 像这样 :

SqlCommand cmd = new SqlCommand("select ExerciseName from ExerciseDisplay   Where TypeName='" + ddlType.SelectedValue + "'", con);

The problem is your event, you event is for the other drop down, your event should be: 问题是你的事件,你的事件是为了另一个下拉,你的事件应该是:

protected void ddlType_SelectedIndexChanged(object sender, EventArgs e)
{
    BindExercise();
}

A contribution to your code, if you populate your dropdown with datasource, the items that you add will not appear if they are before the bind (). 对代码的贡献,如果使用数据源填充下拉列表,如果它们位于bind()之前,则不会显示添加的项目。 You must add the following, to stay but the latest: 您必须添加以下内容,以保留最新内容:

ddlType.Items.Insert(0, "--Please Select country--");

I hope I have been helpful, and good luck with your question. 我希望我一直很有帮助,祝你好运。 (Sorry form my english) (抱歉,我的英文)

问题出现在selectedIndexChanged函数中,当ddltype下拉列表的索引改变而不是dllExericese时,可能会触发该问题,在使用autopost方法后,你应该确保dlltype selectedindex没有改变,方法是在函数dlltype_selectedindexchanged中设置dlltype.SelectedIndex = dlltyp.selectedindex与dlltype下拉列表关联。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM