根据所选类别获取子类别Asp.net C#

时间:2014-07-15 13:11:26

标签: c# asp.net sql-server

我有一个针对品牌的自动填充文本和一个通过数据库填充的类别的dropDown(SQL Server 2008)。这是类别dropDown的代码

SqlCommand cmd = new SqlCommand("SELECT * FROM category", con);
cmd.Connection.Open();

SqlDataReader ddlValues;
ddlValues = cmd.ExecuteReader();

getCategoryOfBrand.DataSource = ddlValues;
getCategoryOfBrand.DataValueField = "category";
getCategoryOfBrand.DataTextField = "category";
getCategoryOfBrand.DataBind();

cmd.Connection.Close();
cmd.Connection.Dispose();

现在我希望当我选择品牌时,它会自动设置所选的类别dropDown =所选品牌的类别。

这是品牌表。

this is brands table.

类别表

enter image description here

品牌文字字段的textChange事件代码

public void getFallingCategory(object sender, EventArgs e)
{

    brandNameUpdated.Text = getBrandForUpdate.Text;
    SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["ravissantCon"].ConnectionString);
    con.Open();
    string getUser = "SELECT category FROM brands WHERE brandName = '" + getBrandForUpdate.Text + "'";

    SqlCommand cmd = new SqlCommand(getUser, con);
    SqlDataReader sdr = cmd.ExecuteReader();

    if (sdr.HasRows)
    {
        while (sdr.Read())
        {
            getCategoryOfBrand.SelectedValue = sdr["category"].ToString();
        }
    }

    sdr.Close();
    con.Close();
}

1 个答案:

答案 0 :(得分:1)

我有一个针对品牌的自动填充文本和一个通过数据库填充的类别的dropDown(SQL Server 2008)。这是类别dropDown的代码

SqlCommand cmd = new SqlCommand("SELECT * FROM category", con);
cmd.Connection.Open();

SqlDataReader ddlValues;
ddlValues = cmd.ExecuteReader();

getCategoryOfBrand.DataSource = ddlValues;
getCategoryOfBrand.DataValueField = "category";
getCategoryOfBrand.DataTextField = "category";
getCategoryOfBrand.DataBind();

cmd.Connection.Close();
cmd.Connection.Dispose();

现在我希望当我选择品牌时,它会自动设置所选的类别dropDown =所选品牌的类别。

这是品牌表。

this is brands table.

类别表

enter image description here

品牌文字字段的textChange事件代码

public void getFallingCategory(object sender, EventArgs e)
{

    brandNameUpdated.Text = getBrandForUpdate.Text;
    SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["ravissantCon"].ConnectionString);
    con.Open();
    string getUser = "SELECT category FROM brands WHERE brandName = '" + getBrandForUpdate.Text + "'";

    SqlCommand cmd = new SqlCommand(getUser, con);
    SqlDataReader sdr = cmd.ExecuteReader();

    if (sdr.HasRows)
    {
        while (sdr.Read())
        {
            getCategoryOfBrand.SelectedValue = sdr["category"].ToString();
        }
    }

    sdr.Close();
    con.Close();
}