在两个级联下拉列表中显示数据库中的相关列

时间:2016-03-26 18:52:34

标签: c# asp.net oracle

我在一个带有c#代码的asp页面上工作,该页面应该在第一个dropDown中显示数据库中的大学名称,然后根据所选择的大学名称显示第二个dropDown中的所有大学名称,问题是在第二个dropDown中显示所有大学的所有大学不仅仅是所选择的大学,这是我的代码,我使用oracle数据库我有一个表包含两个coloumns uni名称和colg名称,它们都是主键。 我希望有人可以帮助我

private void BindDropDownList(DropDownList ddl, string query, string text,string defaultText)


{        

string conString = ConfigurationManager.ConnectionStrings["ConnectionString2"].ConnectionString;

    OracleCommand cmd = new OracleCommand(query);
    using (OracleConnection con = new OracleConnection(conString))
    {
        using (OracleDataAdapter sda = new OracleDataAdapter())
        {
            cmd.Connection = con;
            con.Open();
            ddl.DataSource = cmd.ExecuteReader();
            ddl.DataTextField = text;

            ddl.DataBind();
            con.Close();
        }
    }
    ddl.Items.Insert(0, new ListItem(defaultText, "0"));
}

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        string query = "select UniName from Colg";
        BindDropDownList(ddlUniversities, query, "UniName", "Please Select University");
        ddlColleges.Enabled = false;

        ddlColleges.Items.Insert(0, new ListItem("Please Select College", "0"));

    }
}

protected void University_Changed(object sender, EventArgs e)
{
    ddlColleges.Enabled = false;

    ddlColleges.Items.Clear();

    ddlColleges.Items.Insert(0, new ListItem("Please Select College", "0"));

    string uniName = ddlUniversities.SelectedItem.Value;

    string query = string.Format("select ColgNamef from Colg where UniName = uniName");

        BindDropDownList(ddlColleges, query, "ColgNamef", "Please Select College");
        ddlColleges.Enabled = true;

}
protected void College_Changed(object sender, EventArgs e)
{

}

1 个答案:

答案 0 :(得分:0)

string query = string.Format("select ColgNamef from Colg where UniName = '{0}'",uniName);