最终样式:
1protected void Page_Load(object sender, EventArgs e)
2 {
3 if (!Page.IsPostBack)
4 {
5 BindDrpClass();
6 }
7 }
8 //绑定顶级分类
9 private void BindDrpClass()
10 {
11 Bll.Class classSystem = new Bll.Class();
12 DataTable dt = classSystem.GetClassList("").Tables[0];
13 ddlClass.Items.Clear();
14 ddlClass.Items.Add(new ListItem("添加根栏目","0"));
15 DataRow[] drs = dt.Select("ParentID= " + 0);
16
17 foreach (DataRow dr in drs)
18 {
19 string classid = dr["ClassID"].ToString();
20 string classname = dr["ClassName"].ToString();
21 //顶级分类显示形式
22 classname = "╋" + classname;
23
24 ddlClass.Items.Add(new ListItem(classname, classid));
25 int sonparentid = int.Parse(classid);
26 string blank = "├";
27 //递归子分类方法
28 BindNode(sonparentid, dt, blank);
29 }
30 ddlClass.DataBind();
31 }
32 //绑定子分类
33 private void BindNode(int parentid, DataTable dt, string blank)
34 {
35 DataRow[] drs = dt.Select("ParentID= " + parentid);
36
37 foreach (DataRow dr in drs)
38 {
39 string classid = dr["ClassID"].ToString();
40 string classname = dr["ClassName"].ToString();
41
42 classname = blank + classname;
43 ddlClass.Items.Add(new ListItem(classname, classid));
44
45 int sonparentid = int.Parse(classid);
46 string blank2 = blank + "─";
47
48 BindNode(sonparentid, dt, blank2);
49 }
50 }
2 {
3 if (!Page.IsPostBack)
4 {
5 BindDrpClass();
6 }
7 }
8 //绑定顶级分类
9 private void BindDrpClass()
10 {
11 Bll.Class classSystem = new Bll.Class();
12 DataTable dt = classSystem.GetClassList("").Tables[0];
13 ddlClass.Items.Clear();
14 ddlClass.Items.Add(new ListItem("添加根栏目","0"));
15 DataRow[] drs = dt.Select("ParentID= " + 0);
16
17 foreach (DataRow dr in drs)
18 {
19 string classid = dr["ClassID"].ToString();
20 string classname = dr["ClassName"].ToString();
21 //顶级分类显示形式
22 classname = "╋" + classname;
23
24 ddlClass.Items.Add(new ListItem(classname, classid));
25 int sonparentid = int.Parse(classid);
26 string blank = "├";
27 //递归子分类方法
28 BindNode(sonparentid, dt, blank);
29 }
30 ddlClass.DataBind();
31 }
32 //绑定子分类
33 private void BindNode(int parentid, DataTable dt, string blank)
34 {
35 DataRow[] drs = dt.Select("ParentID= " + parentid);
36
37 foreach (DataRow dr in drs)
38 {
39 string classid = dr["ClassID"].ToString();
40 string classname = dr["ClassName"].ToString();
41
42 classname = blank + classname;
43 ddlClass.Items.Add(new ListItem(classname, classid));
44
45 int sonparentid = int.Parse(classid);
46 string blank2 = blank + "─";
47
48 BindNode(sonparentid, dt, blank2);
49 }
50 }
数据访问(Bll里面的classSystem方法 )
源码下载:
https://files.cnblogs.com/chensubo/RecursionDemo.rar