asp.net DropDownList 三级联动下拉菜单实现代码
家电维修 2023-07-16 19:17www.caominkang.com家电维修技术
复制代码 代码如下:
if (!IsPostBack)
{
//一级分类列表
this.DropDonList1.DataSource = dsbb.SelectSubjct1();
this.DropDonList1.DataTextField = "cName";
this.DropDonList1.DataValueField = "Ccode";
this.DropDonList1.DataBind();
this.DropDonList1.Items.Insert(0,ne ListItem("请选择一级分类","0"));
this.DropDonList8.Items.Insert(0, ne ListItem("请选择二级分类", "0"));
this.DropDonList9.Items.Insert(0,ne ListItem ("请选择三级分类","0"));
//二级分类列表
}
///
/// 绑定二级分类
///
///
///
protected void DropDonList1_SelectedIndexChanged(object sender, EventArgs e)
{
libs.Database.Dbbase dbb = ne libs.Database.Dbbase();
if (Convert.ToInt32(this.DropDonList1.SelectedValue) == 0) //清除列表内容
{
this.DropDonList8.Items.Clear();
this.DropDonList8.Items.Insert(0, ne ListItem("请选择二级分类", "0"));
this.DropDonList9.Items.Clear();
this.DropDonList9.Items.Insert(0, ne ListItem("请选择三级分类", "0"));
}
else //二级分类列表
{
this.DropDonList8.DataSource = dbb.Selectsubjct2(this.DropDonList1.SelectedValue.Substring(0,2));
this.DropDonList8.DataTextField = "cName";
this.DropDonList8.DataValueField = "Ccode";
this.DropDonList8.DataBind();
this.DropDonList8.Items.Insert(0,ne ListItem ("请选择二级分类","0"));
this.DropDonList9.Items.Clear();//清除第三分类
this.DropDonList9.Items.Insert(0, ne ListItem("请选择三级分类", "0"));
}
}
///
/// 绑定三级分类
///
///
///
protected void DropDonList8_SelectedIndexChanged(object sender, EventArgs e)
{
libs.Database.Dbbase dbase = ne libs.Database.Dbbase();
this.DropDonList9.DataSource = dbase.selectsubject3(this.DropDonList8.SelectedValue.Substring(0,4));
this.DropDonList9.DataTextField = "cName";
this.DropDonList9.DataValueField = "Ccode";
this.DropDonList9.DataBind();
this.DropDonList9.Items.Insert(0,ne ListItem("请选择三级分类","0"));
}
Dbbase.cs页
复制代码 代码如下:
///
/// 查询一级栏目
///
///
public DataSet SelectSubjct1()
{
string con = System.Configuration.ConfigurationSettings.AppSettings["sqlconn"];
SqlConnection conn = ne SqlConnection(con);
string sqlstr = "SELECT kndid, Ccode, cName, cLevel FROM kind WHERe cLevel = 1";
DataSet dst = ne DataSet();
SqlDataAdapter sda = ne SqlDataAdapter(sqlstr,conn);
try
{
sda.Fill(dst);
return dst;
}
catch (Exception ex)
{
thro ne Exception(ex.Message);
}
finally
{
conn.Close();
}
}
///
/// 查询二级栏目内容
///
///
///
public DataSet Selectsubjct2(string ode)
{
string conn1 = System.Configuration.ConfigurationSettings.AppSettings["sqlconn"];
SqlConnection conn = ne SqlConnection(conn1);
string sqqq = "select kndid,Ccode,cName,cLevel from kind here cLevel = 2 and Ccode like '" + ode + "%'";
DataSet dss = ne DataSet();
SqlDataAdapter sdd = ne SqlDataAdapter(sqqq,conn);
try
{
sdd.Fill(dss);
return dss;
}
catch (Exception ex)
{
thro ne Exception(ex.Message);
}
finally
{
conn.Close();
}
}
///
/// 查询三级栏目内容
///
///
///
public DataSet selectsubject3(string cde)
{
string conn2 = System.Configuration.ConfigurationSettings.AppSettings["sqlconn"];
SqlConnection conn = ne SqlConnection(conn2);
string sqq = "select kndid,Ccode,cName,cLevel from kind here cLevel = 3 and Ccode like '" + cde + "%'";
DataSet dst = ne DataSet();
SqlDataAdapter sdaa = ne SqlDataAdapter(sqq,conn);
try
{
sdaa.Fill(dst);
return dst;
}
catch (Exception ex)
{
thro ne Exception(ex.Message);
}
finally
{
conn.Close();
}
}
注意DropDonList1_SelectedIndexChanged 事件,AutoPostBack="True"