ASP.NET递归添加树节点

表设计如图:

id        title         parentid

1         asp.net   0

2         c#           0

3         c#_0       2

4         c#_1       3

5         c#_2       4

页面中添加一个TreeView控件

写添加节点方法:

private void AddNode(int id, TreeNode parentnode)

{

string sql = "select * from menu";//sql 语句

DataTable table = DB.GetDB(sql);//获取数据

DataView view = new DataView(table);//把表数据添加到自定义视图中

view.RowFilter = "parentid="+id;//设置视图查询条件

foreach (DataRowView row in view)//遍历视图中数据

   {              TreeNode node = new TreeNode();

     node.Text = row[1].ToString();

int newid = Convert.ToInt32(row[0]);

if (parentnode != null)            
                    {

node.Expanded = false;

parentnode.ChildNodes.Add(node);

AddNode(newid, node);//递归查找节点

}

else

{

TreeView1.Nodes.Add(node);

AddNode(newid, node);

}

}

}

DB类中GetDB()方法:

public static SqlConnection sqlconn()

{         string sqlconnstr = "database=menutree;data source=.;uid=sa;pwd=123";

SqlConnection conn = new SqlConnection(sqlconnstr);         return conn;     }

public static DataTable GetDB(string sql)

{         SqlConnection connection = sqlconn();

SqlCommand comm = new SqlCommand(sql, connection);

SqlDataAdapter ad = new SqlDataAdapter(comm);

DataTable table = new DataTable();

ad.Fill(table);

return table;     }

调用代码:

protected void Page_Load(object sender, EventArgs e)

{                if (!IsPostBack)

{             TreeView1.Nodes.Clear();

AddNode(0, (TreeNode)null);        
}     }

完成!

时间: 2024-08-05 17:29:15

ASP.NET递归添加树节点的相关文章

Winform TreeList递归绑定树节点

/// <summary> /// 绑定树目录 /// </summary> /// <param name="parentId">父ID</param> /// <param name="node">父节点</param> private void BindTree(int parentId, TreeListNode parentNode) { StringBuilder sb = new

如何动态添加树节点(.NET)

很简单: 就两方法: //添加父节点    public void AddNode()    {        KunSoftLogic.WIM.WIM10100 logic_WIM10100 = new KunSoftLogic.WIM.WIM10100();        DataTable dt = new DataTable();        dt = logic_WIM10100.AddPNode().Tables[0];        Node node = new Node();

C# TreeView 树节点:递归显示整个系统盘符文件目录及文件

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows.Forms; using System.IO; namespace Demo { /// <summary> /// 有关对于TreeView节点的填充的相关处理类 /// </summary> class TreeViewUtils { #region 有关将整个系统盘

java递归实现easyui树节点

1. [代码]easyui的Tree节点JSON格式 ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 [{        "id":1,        "text":"Folder1",        "iconCls":"icon-save",        "chi

wxPython treeCtrl树节点递归遍历(复制)

下面是递归遍历.获取树节点的方法. 1 def getTreeAllText(self): 2 '''获取树控件的全部文本''' 3 result = [] 4 treeRoot = self.treeCtrl.GetRootItem() 5 rootText = self.treeCtrl.GetItemText(treeRoot) 6 result.append(u'┌'+rootText) 7 def recursivelyGetTreeItem(tree,leval): 8 ''' 递归

delphi 递归遍历TreeView树节点

//以下全部调试通过,你在窗体扔一个Treeview就行了 procedure GetRootNodes(ATreeView:TTreeView);//得到所有根节点var vNode:TTreeNode;begin  vNode:=nil;  vNode:=ATreeView.Items.GetFirstNode;  While vNode<>nil do  begin    ShowMessage(vNode.Text);//处理查找到的根节点    vNode:=vNode.getNex

树节点

private void zimulu(TreeNode node)//在点开节点的一瞬间,解析当前目录的第三级目录 { try//对错误或者权限不作处理 { //根据获得的完整目录得到该目录下的子目录 foreach (var dirinfor in new DirectoryInfo(node.Tag.ToString()).GetDirectories()) { //依据目录的完整目录获取其子目录,否则找不到文件 //以子目录的文件名为名称创建树节点 var n = new TreeNod

找树节点在二叉树中的深度

/* *pRoot接收要检索的树的根节点,pNode是要确认深度的结点 path存储从根结点到pNode的所有节点,包括了pNode和根节点 */ void findPath(BinaryTreeNode *pRoot, BinaryTreeNode *pNode, vector<int> &path){ if (pRoot == NULL) return; path.push_back(pRoot->m_nValue); if (pRoot == pNode){ //找到了节点

ext 树节点操作

ext 树节点操作 tree :树    node:节点 1.全部展开 tree.expandAll(); 2.全部收缩 tree.collapseAll(); 3.得到父节点 node.parentNode 4.判断是否有父节点 node.parentNode==null 5.判断是否有子节点 node.hasChildNodes() 6.获取下一级所有子节点 node.eachChild(function(child) { }) 7.获取选择的节点 tree.getSelectionMode