<%@ Page Language="C#" AutoEventWireup="true" CodeFile="MSTreeView.aspx.cs" Inherits="MSTreeView" %>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>TreeView一次性加载和异步加载</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<table>
<tr>
<td style="200px; height:600px; vertical-align:top;">
一次性加载<br />
<asp:TreeView ID="TreeView2" runat="server" ShowLines="true">
</asp:TreeView>
</td>
<td style="200px; height:600px; vertical-align:top;">
异步加载<br />
<asp:TreeView ID="TreeView1" runat="server" ShowLines="true"
OnTreeNodePopulate="TreeView1_TreeNodePopulate" EnableClientScript="true" ExpandDepth="0">
</asp:TreeView>
</td>
</tr>
</table>
</ContentTemplate>
</asp:UpdatePanel>
</div>
</form>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class MSTreeView : System.Web.UI.Page
{
protected DataTable dt = createDT();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//一次性加载 适用于小数据量
TreeView2.Nodes.Clear();
CreateLevelTreeView(TreeView2, dt, "module_name", "module_id", "module_fatherid");
TreeView2.ExpandAll();
//异步加载 适用于大数据量
TreeView1.Nodes.Clear();
DataView dv = dt.DefaultView;
dv.RowFilter = " module_fatherid='0' ";
Bind_Root(dv.ToTable(), "module_name", "module_id");
}
}
#region 异步加载TreeView
/// <summary>
/// 绑定父节点
/// </summary>
/// <param name="dt">数据源</param>
/// <param name="text">text</param>
/// <param name="value">value</param>
private void Bind_Root(DataTable dt,string text,string value)
{
TreeNode tn;
for (int i = 0; i < dt.Rows.Count; i++)
{
tn = new TreeNode();
tn.Value = dt.Rows[i][value].ToString();
tn.Text = dt.Rows[i][text].ToString();
//判断是否有子节点
if (Check_Child(tn.Value))
{
tn.PopulateOnDemand = true;
tn.Expanded = false;
}
TreeView1.Nodes.Add(tn);
}
}
/// <summary>
/// 绑定节点的子节点
/// </summary>
/// <param name="treeNode"></param>
/// <param name="p"></param>
private void Bind_Child(TreeNode treeNode, string fatherID, string text, string value)
{
TreeNode tn;
DataRow[] drExist = dt.Select(" module_fatherid= '" + fatherID + "'");
for (int i = 0; i < drExist.Length; i++)
{
tn = new TreeNode();
tn.Value = drExist[i][value].ToString();
tn.Text = drExist[i][text].ToString();
//检验是否有子节点
if (Check_Child(tn.Value))
{
tn.PopulateOnDemand = true;
tn.Expanded = false;
}
treeNode.ChildNodes.Add(tn);
}
}
/// <summary>
/// 判断是否有子节点
/// </summary>
/// <param name="pre_Val">父类ID</param>
/// <returns></returns>
private bool Check_Child(string pre_Val)
{
DataRow[] dr = dt.Select(" module_fatherid= '" + pre_Val + "'");
bool exist = (dr.Length > 0) ? true : false;
return exist;
}
protected void TreeView1_TreeNodePopulate(object sender, TreeNodeEventArgs e)
{
Bind_Child(e.Node, e.Node.Value.ToString(), "module_name", "module_id");
}
#endregion
#region 一次性加载TreeView
/// <summary>
/// 创建无限分级目录树TreeView
/// </summary>
/// <param name="treeview">TreeView空间</param>
/// <param name="dt">数据源DataTable</param>
/// <param name="text">text字段</param>
/// <param name="value">value字段</param>
/// <param name="parentid">深度字段 例如parentid</param>
public static void CreateLevelTreeView(TreeView treeview, DataTable dt, string text, string value, string parentid)
{
DataView dv = dt.DefaultView;
dv.RowFilter = parentid + " ='0'";
foreach (DataRowView drv in dv)
{
TreeNode node = new TreeNode();
node.Text = drv[text].ToString();
node.Value = drv[value].ToString();
node.Expanded = false;
treeview.Nodes.Add(node);
CreatTreeViewChildNode(dv, node, text, value, parentid);
}
}
/// <summary>
/// 递归绑定子节点
/// </summary>
/// <param name="dv">源DataView</param>
/// <param name="parentNode">当前节点</param>
/// <param name="text">text字段</param>
/// <param name="value">value字段</param>
/// <param name="parentid">深度字段 例如parentid</param>
private static void CreatTreeViewChildNode(DataView dv, TreeNode parentNode, string text, string value, string parentid)
{
dv.RowFilter = parentid + " ='" + parentNode.Value + "' ";
foreach (DataRowView row in dv)
{
TreeNode replyNode = new TreeNode();
replyNode.Text = row[text].ToString();
replyNode.Value = row[value].ToString();
replyNode.Expanded = false;
parentNode.ChildNodes.Add(replyNode);
CreatTreeViewChildNode(dv, replyNode, text, value, parentid);
}
}
#endregion
#region 创建数据
private static DataTable createDT()
{
DataTable dt = new DataTable();
dt.Columns.Add("module_id");
dt.Columns.Add("module_name");
dt.Columns.Add("module_fatherid");
dt.Columns.Add("module_url");
dt.Columns.Add("module_order");
dt.Rows.Add("C1", "全国", "0", "", "1");
dt.Rows.Add("M01", "广东", "C1", "", "1");
dt.Rows.Add("M0101", "深圳", "M01", "", "100");
dt.Rows.Add("M010101", "南山区", "M0101", "", "1000");
dt.Rows.Add("M010102", "罗湖区", "M0101", "", "1001");
dt.Rows.Add("M010103", "福田区", "M0101", "", "1002");
dt.Rows.Add("M010104", "宝安区", "M0101", "", "1003");
dt.Rows.Add("M010105", "龙岗区", "M0101", "", "1004");
dt.Rows.Add("M01010301", "上梅林", "M010103", "", "1002001");
dt.Rows.Add("M01010302", "下梅林", "M010103", "", "1002002");
dt.Rows.Add("M01010303", "车公庙", "M010103", "", "1002003");
dt.Rows.Add("M01010304", "竹子林", "M010103", "", "1002004");
dt.Rows.Add("M01010305", "八卦岭", "M010103", "", "1002005");
dt.Rows.Add("M01010306", "华强北", "M010103", "", "1002006");
dt.Rows.Add("M0102", "广州", "M01", "", "101");
dt.Rows.Add("M010201", "越秀区", "M0102", "", "1105");
dt.Rows.Add("M010202", "海珠区", "M0102", "", "1106");
dt.Rows.Add("M010203", "天河区", "M0102", "", "1107");
dt.Rows.Add("M010204", "白云区", "M0102", "", "1108");
dt.Rows.Add("M010205", "黄埔区", "M0102", "", "1109");
dt.Rows.Add("M010206", "荔湾区", "M0102", "", "1110");
dt.Rows.Add("M010207", "罗岗区", "M0102", "", "1111");
dt.Rows.Add("M010208", "南沙区", "M0102", "", "1112");
return dt;
}
#endregion
}