当前位置: 移动技术网 > IT编程>开发语言>.net > asp.net DropDownList 三级联动下拉菜单实现代码

asp.net DropDownList 三级联动下拉菜单实现代码

2017年12月12日  | 移动技术网IT编程  | 我要评论

陈子仪身高,2012319,付首一

复制代码 代码如下:

if (!ispostback)
{
//一级分类列表
this.dropdownlist1.datasource = dsbb.selectsubjct1();
this.dropdownlist1.datatextfield = "cname";
this.dropdownlist1.datavaluefield = "ccode";
this.dropdownlist1.databind();
this.dropdownlist1.items.insert(0,new listitem("请选择一级分类","0"));
this.dropdownlist8.items.insert(0, new listitem("请选择二级分类", "0"));
this.dropdownlist9.items.insert(0,new listitem ("请选择三级分类","0"));
//二级分类列表

}
/// <summary>
/// 绑定二级分类
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void dropdownlist1_selectedindexchanged(object sender, eventargs e)
{
libs.database.dbbase dbb = new libs.database.dbbase();
if (convert.toint32(this.dropdownlist1.selectedvalue) == 0) //清除列表内容
{
this.dropdownlist8.items.clear();
this.dropdownlist8.items.insert(0, new listitem("请选择二级分类", "0"));
this.dropdownlist9.items.clear();
this.dropdownlist9.items.insert(0, new listitem("请选择三级分类", "0"));
}
else //二级分类列表
{
this.dropdownlist8.datasource = dbb.selectsubjct2(this.dropdownlist1.selectedvalue.substring(0,2));
this.dropdownlist8.datatextfield = "cname";
this.dropdownlist8.datavaluefield = "ccode";
this.dropdownlist8.databind();
this.dropdownlist8.items.insert(0,new listitem ("请选择二级分类","0"));
this.dropdownlist9.items.clear();//清除第三分类
this.dropdownlist9.items.insert(0, new listitem("请选择三级分类", "0"));
}
}
/// <summary>
/// 绑定三级分类
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void dropdownlist8_selectedindexchanged(object sender, eventargs e)
{
libs.database.dbbase dbase = new libs.database.dbbase();
this.dropdownlist9.datasource = dbase.selectsubject3(this.dropdownlist8.selectedvalue.substring(0,4));
this.dropdownlist9.datatextfield = "cname";
this.dropdownlist9.datavaluefield = "ccode";
this.dropdownlist9.databind();
this.dropdownlist9.items.insert(0,new listitem("请选择三级分类","0"));
}


dbbase.cs页:
复制代码 代码如下:

/// <summary>
/// 查询一级栏目
/// </summary>
/// <returns></returns>
public dataset selectsubjct1()
{
string con = system.configuration.configurationsettings.appsettings["sqlconn"];
sqlconnection conn = new sqlconnection(con);
string sqlstr = "select kndid, ccode, cname, clevel from kind where clevel = 1";
dataset dst = new dataset();
sqldataadapter sda = new sqldataadapter(sqlstr,conn);
try
{
sda.fill(dst);
return dst;
}
catch (exception ex)
{
throw new exception(ex.message);
}
finally
{
conn.close();
}
}
/// <summary>
/// 查询二级栏目内容
/// </summary>
/// <param name="ccode"></param>
/// <returns></returns>
public dataset selectsubjct2(string ccode)
{
string conn1 = system.configuration.configurationsettings.appsettings["sqlconn"];
sqlconnection conn = new sqlconnection(conn1);
string sqqq = "select kndid,ccode,cname,clevel from kind where clevel = 2 and ccode like '" + ccode + "%'";
dataset dss = new dataset();
sqldataadapter sdd = new sqldataadapter(sqqq,conn);
try
{
sdd.fill(dss);
return dss;
}
catch (exception ex)
{
throw new exception(ex.message);
}
finally
{
conn.close();
}
}
/// <summary>
/// 查询三级栏目内容
/// </summary>
/// <param name="cde"></param>
/// <returns></returns>
public dataset selectsubject3(string cde)
{
string conn2 = system.configuration.configurationsettings.appsettings["sqlconn"];
sqlconnection conn = new sqlconnection(conn2);
string sqq = "select kndid,ccode,cname,clevel from kind where clevel = 3 and ccode like '" + cde + "%'";
dataset dst = new dataset();
sqldataadapter sdaa = new sqldataadapter(sqq,conn);
try
{
sdaa.fill(dst);
return dst;
}
catch (exception ex)
{
throw new exception(ex.message);
}
finally
{
conn.close();
}
}

注意:dropdownlist1_selectedindexchanged 事件,autopostback="true"

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网