树形填充下拉列表
之前每次用到都是重写,加上有对旧代码推倒重来的喜好,特以此备注,省却麻烦。
1 /// <summary>
2 /// 绑定树形结构到下拉框
3 /// </summary>
4 /// <param name="table">数据源</param>
5 /// <param name="pID">父节点ID</param>
6 /// <param name="sLink">/┗/┣</param>
7 protected void BindTreeToSelect(DataTable table,string pID,string sLink)
8 {
9 DataRow[] rows = table.Select(" ParentID = " + pID);
10
11 string textName = string.Empty;
12 string valueID = string.Empty;
13
14 foreach (DataRow row in rows) {
15 textName = sLink + row["Name"].ToString();
16 valueID = row["ID"].ToString();
17 //添加列表选项
18 this.ddlParentID.Items.Add(new ListItem(textName, valueID));
19
20 BindTreeToSelect(table, valueID, sLink.Insert(0, HttpUtility.HtmlDecode(" ")));
21 }
2 /// 绑定树形结构到下拉框
3 /// </summary>
4 /// <param name="table">数据源</param>
5 /// <param name="pID">父节点ID</param>
6 /// <param name="sLink">/┗/┣</param>
7 protected void BindTreeToSelect(DataTable table,string pID,string sLink)
8 {
9 DataRow[] rows = table.Select(" ParentID = " + pID);
10
11 string textName = string.Empty;
12 string valueID = string.Empty;
13
14 foreach (DataRow row in rows) {
15 textName = sLink + row["Name"].ToString();
16 valueID = row["ID"].ToString();
17 //添加列表选项
18 this.ddlParentID.Items.Add(new ListItem(textName, valueID));
19
20 BindTreeToSelect(table, valueID, sLink.Insert(0, HttpUtility.HtmlDecode(" ")));
21 }
22 }
调用代码:BindTreeToSelect(ds.Tables[0], "0", "");
效果图如: