asp.net DropDownList 三级联动下拉菜单实现代码
程序员文章站
2024-03-08 16:42:35
复制代码 代码如下: if (!ispostback) { //一级分类列表 this.dropdownlist1.datasource = dsbb.selectsubj...
复制代码 代码如下:
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"
推荐阅读
-
asp.net DropDownList 三级联动下拉菜单实现代码
-
asp.net省市三级联动的DropDownList+Ajax的三种框架(aspnet/Jquery/ExtJs)示例
-
asp.net下使用AjaxPro实现二级联动代码
-
asp.net省市三级联动的DropDownList+Ajax的三种框架(aspnet/Jquery/ExtJs)示例
-
asp.net下使用AjaxPro实现二级联动代码
-
asp.net中通过DropDownList的值去控制TextBox是否可编写的实现代码
-
asp.net中通过DropDownList的值去控制TextBox是否可编写的实现代码
-
Yii2使用dropdownlist实现地区三级联动功能的方法
-
asp.net DropDownList实现二级联动效果
-
js实现年月日三级下拉菜单联动