dropdownlist 实现无限级树形菜单
时间:2010-11-12 来源:大菜
private void BindDrpClass()
{
//获得所有数据列表
DataTable dt = mh.returnds_bysting("select * from TB_SupplyType ").Tables [0];
ddl . Items.Clear();
ddl.Items.Add(new ListItem("选择类别", "0"));
DataRow[] drs = dt.Select("s_Sid= " + 0);
foreach (DataRow dr in drs)
{
string classid = dr["s_Id"].ToString();
string classname = dr["s_Title"].ToString();
//顶级分类显示形式
classname = "" + classname;
ddl.Items.Add(new ListItem(classname, classid));
int sonparentid = int.Parse(classid);
string blank = " ";
//递归子分类方法
BindNode(sonparentid, dt, blank);
}
ddl.DataBind();
}
private void BindNode(int parentid, DataTable dt, string blank)
{
DataRow[] drs = dt.Select("s_Sid= " + parentid);
foreach (DataRow dr in drs)
{
string classid = dr["s_Id"].ToString();
string classname = dr["s_Title"].ToString();
classname = blank + classname;
ddl.Items.Add(new ListItem(classname, classid));
int sonparentid = int.Parse(classid);
string blank2 = blank + "-";
BindNode(sonparentid, dt, blank2);
}
}
虽然能实现无限级分类,但是样式有点逊色,有兴趣的朋友可以加个css 控制一下