一般情况下生成TreeView我们用的是用递归不建议用递归数据量大了会慢,
小弟今天用SortedList集合实现一下没有技术含量,一看代码大家就会明白。个人也比较喜欢这种方法,数据量大了执行速度也可以。
下一篇方法动态生成TreeView(二)是用递归实现的,有时间可以看一下。
思路:
把节点放到SortedList,里再根据标识找到父节点,加上相应的子节点。
只是一个小例子,有很多方面不足,可以在这个例子上扩展,传参什么的可以自己在数据库里建,等等,让这个例子变成你自己的东西。
效果图:
前台页面:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="TreeViewPage.aspx.cs" Inherits="WebApplication3.TreeViewPage" %> <!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:TreeView ID="tv_Method" runat="server" Width="200px" > </asp:TreeView> </div> </form></body></html>
后台代码:
using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;using System.Data;using System.Collections;namespace WebApplication3{public partial class TreeViewPage : System.Web.UI.Page {private DataTable dt;protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { dt = CreateTable(); CreateTree(); } } #region 生成树public void CreateTree() {try {//SortedList集合表示键/值对的集合,//这些键值对按键排序并可按照键和索引访问.如果不懂自己去查msdn SortedList m_SortedList = new SortedList(); TreeNode treeNode = new TreeNode();if (dt.Rows.Count <= 0) { treeNode.Text = "为空"; treeNode.Value = ""; treeNode.NavigateUrl = ""; treeNode.Target = ""; treeNode.Expanded = false; treeNode.SelectAction = TreeNodeSelectAction.SelectExpand; }else {for (int i = 0; i < dt.Rows.Count; i++) {//创建父节点 if (dt.Rows[i]["Grade"].ToString() == "1") { TreeNode node = new TreeNode(); node.Text = dt.Rows[i]["MenuName"].ToString(); node.Value = dt.Rows[i]["MenuID"].ToString();if (dt.Rows[i]["Flag"].ToString() == "1") { node.NavigateUrl = dt.Rows[i]["href"].ToString(); treeNode.Target = "_blank"; } node.Expanded = false; node.SelectAction = TreeNodeSelectAction.Expand; m_SortedList.Add(node.Value, node); tv_Method.Nodes.Add(node); }else {//找到父节点 TreeNode parentNode = (TreeNode)m_SortedList.GetByIndex(m_SortedList.IndexOfKey(dt.Rows[i]["ParentMenu"].ToString()));//创建子节点 TreeNode childNode = new TreeNode(); childNode.Text = dt.Rows[i]["MenuName"].ToString(); childNode.Value = dt.Rows[i]["MenuID"].ToString();if (dt.Rows[i]["Flag"].ToString() == "1") { childNode.NavigateUrl = dt.Rows[i]["href"].ToString(); treeNode.Target = "_blank"; } childNode.Expanded = false; childNode.SelectAction = TreeNodeSelectAction.Expand; m_SortedList.Add(childNode.Value, childNode); parentNode.ChildNodes.Add(childNode); } } } }catch (Exception ex) {throw ex; } }#endregion#region 创建Menu表public DataTable CreateTable() {try { DataTable dt = new DataTable(); dt.Columns.Add("MenuID", typeof(string)); dt.Columns.Add("MenuName", typeof(string)); dt.Columns.Add("ParentMenu", typeof(string));//父节点 dt.Columns.Add("Grade", typeof(int));//标识这是第几级节点 dt.Columns.Add("Flag", typeof(int));//标识,是不是尾结点,1是 0 不是 dt.Columns.Add("href", typeof(string));//指向的链接 DataRow dr = dt.NewRow(); dr["MenuID"] = "Menu01"; dr["MenuName"] = "第一级"; dr["ParentMenu"] = ""; dr["Grade"] = 1; dr["Flag"] = 1; dr["href"] = "one.aspx"; dt.Rows.Add(dr); dr = dt.NewRow(); dr["MenuID"] = "Menu02"; dr["MenuName"] = "第一级"; dr["ParentMenu"] = ""; dr["Grade"] = 1; dr["Flag"] = 0; dr["href"] = ""; dt.Rows.Add(dr); dr = dt.NewRow(); dr["MenuID"] = "Menu0201"; dr["MenuName"] = "第一下一级"; dr["ParentMenu"] = "Menu02"; dr["Grade"] = 2; dr["Flag"] = 1; dr["href"] = "one.aspx"; dt.Rows.Add(dr); dr = dt.NewRow(); dr["MenuID"] = "Menu0202"; dr["MenuName"] = "第一下一级"; dr["ParentMenu"] = "Menu02"; dr["Grade"] = 2; dr["Flag"] = 1; dr["href"] = ""; dt.Rows.Add(dr); dr = dt.NewRow(); dr["MenuID"] = "Menu020201"; dr["MenuName"] = "第一下一下一级"; dr["ParentMenu"] = "Menu0202"; dr["Grade"] = 2; dr["Flag"] = 1; dr["href"] = "one.aspx"; dt.Rows.Add(dr); dr = dt.NewRow(); dr["MenuID"] = "Menu03"; dr["MenuName"] = "第一级"; dr["ParentMenu"] = ""; dr["Grade"] = 1; dr["Flag"] = 1; dr["href"] = "one.aspx"; dt.Rows.Add(dr);return dt; }catch (Exception ex) {throw ex; } }#endregion }}
时间: 2024-09-26 22:10:35