代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
Asp.Net
】
TreeView控件无限级 一次性加载和异步加载剖析
作者:
Dezai.CN
/ 发布于
2013/5/12
/
542
<%@ 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="width:200px; height:600px; vertical-align:top;"> 一次性加载 <asp:TreeView ID="TreeView2" runat="server" ShowLines="true"> </asp:TreeView> </td> <td style="width:200px; height:600px; vertical-align:top;"> 异步加载 <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 }
试试其它关键字
TreeView
同语言下
.
gzip压缩
.
实现http多线程断点续传下载文件
.
实现多线程断点续传下载大文件
.
生成字符串的 CheckSum
.
根据 UserAgent 获取浏览器的类型和版本
.
根据 Agent 判断是否是智能手机
.
隐藏手机号中间四位为*方法
.
合并图片(二维码和其他图片合并)
.
ASP.NET CORE中判断是否移动端打开网页
.
ASP.NET(C#)实现页面计时(定时)自动跳转
可能有用的
.
C#实现的html内容截取
.
List 切割成几份 工具类
.
SQL查询 多列合并成一行用逗号隔开
.
一行一行读取txt的内容
.
C#动态修改文件夹名称(FSO实现,不移动文件)
.
c# 移动文件或文件夹
.
c#图片添加水印
.
Java PDF转换成图片并输出给前台展示
.
网站后台修改图片尺寸代码
.
处理大图片在缩略图时的展示
Dezai.CN
贡献的其它代码
(
4037
)
.
多线程Socket服务器模块
.
生成随机密码
.
清除浮动样式
.
弹出窗口居中
.
抓取url的函数
.
使用base HTTP验证
.
div模拟iframe嵌入效果
.
通过header转向的方法
.
Session操作类
.
执行sqlite输入插入操作后获得自动编号的ID
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3