Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebAppFL
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
BindDropDownList(this.AreaSource);
}
/// <summary>
/// 绑定分类下拉列表
/// </summary>
private void BindDropDownList(List<TreeType> myList)
{
this.TParentCate.Items.Add(new ListItem("◇根目录", "0"));
BindDropListByList(myList);
}
/// <summary>
/// List<T>里过滤根基点
/// </summary>
/// <param name="m">结构树</param>
/// <returns>T/F</returns>
private bool FindMatch(TreeType m)
{
if (m.ParentID == 0)
{
return true;
}
return false;
}
/// <summary>
/// 按层次绑定下拉框
/// </summary>
/// <param name="mList">结构树</param>
private void BindDropListByList(List<TreeType> mList)
{
string chrFlag = "┆┄◇";
List<TreeType> list = mList.FindAll(FindMatch);
foreach (TreeType m in list)
{
this.TParentCate.Items.Add(new ListItem(chrFlag + m.Name, m.ID.ToString()));
RecursionBind(mList, m.ID, ref chrFlag);
}
}
/// <summary>
/// 绑定类
/// </summary>
/// <param name="mList">结构树</param>
/// <param name="mCateID">树编号</param>
/// <param name="chr">分割符</param>
private void RecursionBind(List<TreeType> mList, int mCateID, ref string chr)
{
foreach (TreeType m in mList)
{
if (m.ParentID == mCateID)
{
chr = "┆┄" + chr;
this.TParentCate.Items.Add(new ListItem(chr + m.Name, m.ID.ToString()));
RecursionBind(mList, m.ID, ref chr);
}
}
if (chr != "┆┄◇")
chr = chr.Remove(0,2);
}
/// <summary>
/// 测试数据源
/// </summary>
private List<TreeType> AreaSource
{
get
{
List<TreeType> TreeList = new List<TreeType>();
TreeList.Add(new TreeType(1, 0, "湖北"));
TreeList.Add(new TreeType(2, 0, "江苏"));
TreeList.Add(new TreeType(3, 0, "浙江"));
TreeList.Add(new TreeType(4, 1, "黄冈"));
TreeList.Add(new TreeType(5, 4, "黄冈镇级"));
TreeList.Add(new TreeType(6, 5, "黄冈村"));
TreeList.Add(new TreeType(7, 3, "杭州"));
TreeList.Add(new TreeType(8, 2, "南京"));
TreeList.Add(new TreeType(9, 0, "北京"));
TreeList.Add(new TreeType(10, 9, "海淀"));
TreeList.Add(new TreeType(11, 10, "中关村"));
TreeList.Add(new TreeType(12, 10, "上地"));
return TreeList;
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace WebAppFL
{
public class TreeType
{
private int _ID;
public int ID
{
get { return _ID; }
set { _ID = value; }
}
private string _Name;
public string Name
{
get { return _Name; }
set { _Name = value; }
}
private int _ParentID;
public int ParentID
{
get { return _ParentID; }
set { _ParentID = value; }
}
public TreeType(int id, int parentid, string name)
{
// TODO: Complete member initialization
this._ID = id;
this._Name = name;
this._ParentID = parentid;
}
}
}
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebAppFL.WebForm1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DropDownList ID="TParentCate" runat="server"></asp:DropDownList>
</div>
</form>
</body>
</html>
