主要使用递归实现,数据库结构:
最终样式:
1
protected
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 }
源码下载:
http://files.cnblogs.com/chensubo/RecursionDemo.rar
改造过的函数
// <asp:DropDownList ID="editparentid" runat="server" ></asp:DropDownList>
///
<summary>
///
加载页面
///
</summary>
protected
void
Page_Load(
object
sender, EventArgs e)
{
BindDrpClass(editparentid);}
/// <summary>
///
创建树
///
</summary>
public
void
BindDrpClass(DropDownList ddlClass)
{
DataTable dt
=
DbHelper.ExecuteDataset(SysConst.DBNAME_XIAOWU,
"
select * from NT_Clcs_ClientType
"
).Tables[
0
];
ddlClass.Items.Clear();
ddlClass.Items.Add(
new
ListItem(
"
添加根栏目
"
,
"
0
"
));
DataRow[] drs
=
dt.Select(
"
ParentID=
"
+
0
);
foreach
(DataRow dr
in
drs){
string
classid
=
dr[
"
Cc_ID
"
].ToString();
string
classname
=
dr[
"
Cc_Name
"
].ToString();
//
顶级分类显示形式
classname
=
"
╋
"
+
classname;
ddlClass.Items.Add(
new
ListItem(classname, classid));
int
sonparentid
=
int
.Parse(classid);
string
blank
=
"
├
"
;
//
递归子分类方法
BindNode(ddlClass, sonparentid, dt, blank);
}
editparentid.DataBind();
}
///
<summary>
///
创建树结点
///
</summary>
private
void
BindNode(DropDownList ddlNode,
int
parentid, DataTable dt,
string
blank)
{
DataRow[] drs
=
dt.Select(
"
ParentID=
"
+
parentid);
foreach
(DataRow dr
in
drs){
string
classid
=
dr[
"
Cc_ID
"
].ToString();
string
classname
=
dr[
"
Cc_Name
"
].ToString();
classname
=
blank
+
classname;
ddlNode.Items.Add(
new
ListItem(classname, classid));
int
sonparentid
=
int
.Parse(classid);
string
blank2
=
blank
+
"
─
"
;
BindNode(ddlNode,sonparentid, dt, blank2);
}
}
二次改造做成一个通用的类 调用 如: new DrpGradingList("select * from dbo.NT_Clcs_ClientType ", "Cc_Name", "Cc_ID", defValue).BindDrpClass(ddl);
///
<summary>
///
DropDownList 树形显示
///
</summary>
public
class DrpGradingList
{
private
string Sql =
"";
private
string Text =
"";
private
string Value =
"";
private
string defValue =
"";
private
string parentId =
"
ParentID
";
///
<summary>
///
DropDownList 树形显示
///
</summary>
///
<param name="sql">
SQL语句
</param>
///
<param name="text">
显示的文本( 数据库字段)
</param>
///
<param name="value">
显示的文本对应的值( 数据库字段)
</param>
public DrpGradingList(
string sql,
string text,
string value,
string defvalue)
{
this.Sql = sql;
this.Text = text;
this.Value = value;
this.defValue = defvalue;
}
///
<summary>
///
DropDownList 树形显示
///
</summary>
///
<param name="sql">
SQL语句
</param>
///
<param name="text">
显示的文本( 数据库字段)
</param>
///
<param name="value">
显示的文本对应的值( 数据库字段)
</param>
public DrpGradingList(
string sql,
string text,
string value,
string defvalue,
string pid)
{
this.Sql = sql;
this.Text = text;
this.Value = value;
this.defValue = defvalue;
this.parentId = pid;
}
///
<summary>
///
创建树
///
</summary>
public
void BindDrpClass(DropDownList ddlClass)
{
DataTable dt = DbHelper.ExecuteDataset(SysConst.DBNAME_XIAOWU, Sql).Tables[
0];
ddlClass.Items.Clear();
ddlClass.Items.Add(
new ListItem(
"
顶级栏目
",
"
0
"));
DataRow[] drs = dt.Select(
""+
this.parentId+
"
=
" +
0);
foreach (DataRow dr
in drs)
{
string classid = dr[
this.Value].ToString();
string classname = dr[
this.Text].ToString();
//
顶级分类显示形式
classname =
"
╋
" + classname;
ListItem itme =
new ListItem();
itme.Text=classname;
itme.Value=classid;
if (itme.Value.Equals(
this.defValue))
itme.Selected =
true;
ddlClass.Items.Add(itme);
int sonparentid =
int.Parse(classid);
string blank =
"
├
";
//
递归子分类方法
BindNode(ddlClass, sonparentid, dt, blank);
}
ddlClass.SelectedIndex = ddlClass.Items.IndexOf(ddlClass.Items.FindByValue(
this.defValue));
ddlClass.DataBind();
}
///
<summary>
///
创建树 ddlClass.Items.Add(new ListItem("顶级栏目", "0"));
///
</summary>
public
void BindDrpClass(DropDownList ddlClass,
bool defItem)
{
DataTable dt = DbHelper.ExecuteDataset(SysConst.DBNAME_XIAOWU, Sql).Tables[
0];
ddlClass.Items.Clear();
if (defItem){
ddlClass.Items.Add(
new ListItem(
"
顶级栏目
",
"
0
"));
}
DataRow[] drs = dt.Select(
"" +
this.parentId +
"
=
" +
0);
foreach (DataRow dr
in drs)
{
string classid = dr[
this.Value].ToString();
string classname = dr[
this.Text].ToString();
//
顶级分类显示形式
classname =
"
╋
" + classname;
ListItem itme =
new ListItem();
itme.Text = classname;
itme.Value = classid;
if (itme.Value.Equals(
this.defValue))
itme.Selected =
true;
ddlClass.Items.Add(itme);
int sonparentid =
int.Parse(classid);
string blank =
"
├
";
//
递归子分类方法
BindNode(ddlClass, sonparentid, dt, blank);
}
ddlClass.SelectedIndex = ddlClass.Items.IndexOf(ddlClass.Items.FindByValue(
this.defValue));
ddlClass.DataBind();
}
public
void BindDrpClass(DropDownList ddlClass,
bool defItem,
string parentIdefVale)
{
if (parentIdefVale.Length ==
0) parentIdefVale =
"
0
";
DataTable dt = DbHelper.ExecuteDataset(SysConst.DBNAME_XIAOWU, Sql).Tables[
0];
ddlClass.Items.Clear();
if (defItem)
{
ddlClass.Items.Add(
new ListItem(
"
顶级栏目
",
"
0
"));
}
DataRow[] drs = dt.Select(
"" +
this.parentId +
"
=
" + parentIdefVale);
foreach (DataRow dr
in drs)
{
string classid = dr[
this.Value].ToString();
string classname = dr[
this.Text].ToString();
//
顶级分类显示形式
classname =
"
╋
" + classname;
ListItem itme =
new ListItem();
itme.Text = classname;
itme.Value = classid;
if (itme.Value.Equals(
this.defValue))
itme.Selected =
true;
ddlClass.Items.Add(itme);
int sonparentid =
int.Parse(classid);
string blank =
"
├
";
//
递归子分类方法
BindNode(ddlClass, sonparentid, dt, blank);
}
ddlClass.SelectedIndex = ddlClass.Items.IndexOf(ddlClass.Items.FindByValue(
this.defValue));
ddlClass.DataBind();
}
///
<summary>
///
创建树结点
///
</summary>
private
void BindNode(DropDownList ddlNode,
int parentid, DataTable dt,
string blank)
{
DataRow[] drs = dt.Select(
""+
this.parentId+
"
=
" + parentid);
foreach (DataRow dr
in drs)
{
string classid = dr[
this.Value].ToString();
string classname = dr[
this.Text].ToString();
classname = blank + classname;
ListItem itme =
new ListItem();
itme.Text = classname;
itme.Value = classid;
if (itme.Value.Equals(
this.defValue))
itme.Selected =
true;
ddlNode.Items.Add(itme);
int sonparentid =
int.Parse(classid);
string blank2 = blank +
"
─
";
BindNode(ddlNode, sonparentid, dt, blank2);
}
}
}
RadTreeView控件 应用:
<
telerik:RadTreeView
runat
="server"
ID
="RadTreeView2"
Skin
="Office2007"
Width
="300px"
Height
="450px"
CheckBoxes
="True"
DataTextField
="ParentID"
TriStateCheckBoxes
="False"
>
</
telerik:RadTreeView
>
AddTree(
"
0
"
,
this
.RadTreeView2.Nodes);
public
void
AddTree(
string
fdm, RadTreeNodeCollection nodes)
{
DataTable dt
=
DbHelper.ExecuteDataset(SysConst.DBNAME_XIAOWU,
"
select * from NT_Clcs_Village where ParentID=
"
+
fdm
+
""
).Tables[
0
];
foreach
(DataRow dr
in
dt.Rows)
{
string
classid
=
dr[
"
Cv_ID
"
].ToString();
string
classname
=
dr[
"
Cv_Name
"
].ToString();
RadTreeNode main
=
new
RadTreeNode();
main.Text
=
classname;
main.Value
=
classid;
nodes.Add(main);
AddTree(classid, main.Nodes);
}
}
其它:
dropDownList.SelectedIndex=dropDownList.Items.IndexOf(dropDownList.Items.FindByText(entityBase.Bugstatus));
MVC树形:
使用: @Html.DropDownList("ddlXzqy", ViewData["ddlXzqy"] as IEnumerable<SelectListItem>)
/// <summary>
/// 创建树
/// </summary>
public void BindDrpClass(Guid selectId)
{
var list = repo.GetXzqyRootsList();
List<SelectListItem> selitem = new List<SelectListItem>();
foreach (var item in list)
{
//顶级分类显示形式
if (selectId == item.ID)
{
selitem.Add(new SelectListItem { Text = "╋" + item.Name, Value = item.ID.ToString(), Selected = true });
this.ViewData["selected"] = item.ID.ToString();
}
else
{
selitem.Add(new SelectListItem { Text = "╋" + item.Name, Value = item.ID.ToString() });
}
//递归子分类方法
string blank = "├";
BindNode(item.ID, blank, selitem, selectId);
}
selitem.Insert(0, new SelectListItem { Text = "==行政区域==", Value = "-1" });
ViewData["ddlXzqy"] = selitem;
}
private void BindNode(Guid parentid, string blank, List<SelectListItem> selitem, Guid selectId)
{
var list = repo.GetXzqyChildList(parentid);
foreach (var item in list)
{
if (selectId == item.ID)
{
selitem.Add(new SelectListItem { Text = blank + item.Name, Value = item.ID.ToString(), Selected = true });
this.ViewData["selected"] = item.ID.ToString();
}
else
{
selitem.Add(new SelectListItem { Text = blank + item.Name, Value = item.ID.ToString() });
}
string blank2 = blank + "─";
BindNode(item.ID, blank2, selitem, selectId);
}
}