利用TreeView控件动态生成无限级树
2006-08-09 11:37
387 查看
利用TreeView控件动态生成无限级树
项目需要,有一个树形菜单需要动态生成,联想到TreeView控件,决定用TreeView来实现
首先注意到了TreeView控件有一个属性TreeNodeSrc
这个属性可以指定一个固定格式的xml文件
<?xml version="1.0" encoding="utf-8" ?>
<TREENODES>
<TREENODE text="aaaaaaaa" CheckBox="true"></TREENODE>
<TREENODE text="bbbbbbbb" CheckBox="true"></TREENODE>
<TREENODE text="cccccccccc" EXPANDED="true" CheckBox="true">
<TREENODE text="ddddddddd" CheckBox="true"></TREENODE>
<TREENODE text="eeeeeeeee" CheckBox="true"></TREENODE>
</TREENODE>
<TREENODE text="fffffffffffff" CheckBox="true"></TREENODE>
</TREENODES>于是就想把数据库里的文件读出来写入一个xml文件中,然后再进行绑定
既然是无限级菜单,肯定要用到递归来实现,于是就写了一个如下的递归算法
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
private void CreateXml(XmlDocument objXMLDoc, XmlElement objRootElem, int belong)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//Get DataSet 这里的DataSet具体获取方法我省略掉了
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
DataSet ds = new DataSet();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
foreach(DataRow dr in ds.Tables[0].Rows)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//Create ChildNode TreeNode
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
XmlElement objXmlElem = objXMLDoc.CreateElement("TREENODE");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
objRootElem.AppendChild(objXmlElem);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//Create Attributes Text
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
XmlAttribute objXmlAttText = objXMLDoc.CreateAttribute("Text");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
objXmlAttText.Value = dr["text"].ToString().Trim();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
objXmlElem.SetAttributeNode(objXmlAttText);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//Create Attributes CheckBox
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
XmlAttribute objXmlAttCB = objXMLDoc.CreateAttribute("CheckBox");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
objXmlAttCB.Value = "True";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
objXmlElem.SetAttributeNode(objXmlAttCB);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int id = int.Parse(dr["id"].ToString().Trim());
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
CreateXml(objXMLDoc, objXmlElem, id);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
然后用下面的方法来实现绑定
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
private void BindXmlTree()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//Create Xml File
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
XmlDocument objXmlDoc = new XmlDocument();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//Insert Xml Declaration
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
XmlDeclaration objXmlDeclare = objXmlDoc.CreateXmlDeclaration("1.0", "UTF-8", "yes");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
objXmlDoc.InsertBefore(objXmlDeclare, objXmlDoc.DocumentElement);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
XmlElement objRootElem = objXmlDoc.CreateElement("TREENODES");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
objXmlDoc.AppendChild(objRootElem);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
CreateXml(objXmlDoc, objRootElem, 0);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
objXmlDoc.Save("E:/TreeMenu.xml");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
TreeView2.TreeNodeSrc = "TreeMenu.xml";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
到最后两行我就停住了,因为问题出现了
生成的objXmlDoc文件不知道用什么方式才能绑定上TreeNodeSrc
我试了试直接 TreeView2.TreeNodeSrc = objXmlDoc;
可是TreeNodeSrc是String型的数据,没办法
有没有什么方法可以不保存这个objXmlDoc文件而直接绑定呢?
于是就换了个办法来实现
TreeView有一个节点控件叫TreeNode,直接把数据给TreeNode上
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
private void CreateTree(int belong, Microsoft.Web.UI.WebControls.TreeNode rootnode)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
DataSet ds = new DataSet();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
foreach(DataRow dr in ds.Tables[0].Rows)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Microsoft.Web.UI.WebControls.TreeNode treenode = new Microsoft.Web.UI.WebControls.TreeNode();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
treenode.Text = dr["text"].ToString().Trim();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
treenode.CheckBox = true;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
treenode.Expanded = true;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
rootnode.Nodes.Add(treenode);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int id = int.Parse(dr["id"].ToString().Trim());
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
CreateTree(id, treenode);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
用的是一样的递归思想,而且我发现这样做还比较简单一些,很多TreeNode自带的属性很方便修改
如果用Xml还要手动去添加每一个属性,麻烦
在PageLoad里触发这个方法
private void Page_Load(object sender, System.EventArgs e)
{
Microsoft.Web.UI.WebControls.TreeNode rootnode = new Microsoft.Web.UI.WebControls.TreeNode();
rootnode.Expanded = true;
TreeView2.Nodes.Add(rootnode);
CreateTree(0, rootnode);
}注意,这里为了实现递归,不得不添加一个空的根节点rootnode
这样就算大致实现了
另,数据库里的数据格式是这样的
-----------------------------------------
id text belong
1 aaaa 0
2 bbbb 0
3 cccc 0
4 dddd 1
5 eeee 2
6 ffff 4
posted on 2004-10-28 22:32 流浪的狗 阅读(15011) 评论(36) 编辑 收藏 引用 收藏至365Key 所属分类: Dotnet C#
项目需要,有一个树形菜单需要动态生成,联想到TreeView控件,决定用TreeView来实现
首先注意到了TreeView控件有一个属性TreeNodeSrc
这个属性可以指定一个固定格式的xml文件
<?xml version="1.0" encoding="utf-8" ?>
<TREENODES>
<TREENODE text="aaaaaaaa" CheckBox="true"></TREENODE>
<TREENODE text="bbbbbbbb" CheckBox="true"></TREENODE>
<TREENODE text="cccccccccc" EXPANDED="true" CheckBox="true">
<TREENODE text="ddddddddd" CheckBox="true"></TREENODE>
<TREENODE text="eeeeeeeee" CheckBox="true"></TREENODE>
</TREENODE>
<TREENODE text="fffffffffffff" CheckBox="true"></TREENODE>
</TREENODES>于是就想把数据库里的文件读出来写入一个xml文件中,然后再进行绑定
既然是无限级菜单,肯定要用到递归来实现,于是就写了一个如下的递归算法
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
private void CreateXml(XmlDocument objXMLDoc, XmlElement objRootElem, int belong)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//Get DataSet 这里的DataSet具体获取方法我省略掉了
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
DataSet ds = new DataSet();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
foreach(DataRow dr in ds.Tables[0].Rows)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//Create ChildNode TreeNode
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
XmlElement objXmlElem = objXMLDoc.CreateElement("TREENODE");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
objRootElem.AppendChild(objXmlElem);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//Create Attributes Text
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
XmlAttribute objXmlAttText = objXMLDoc.CreateAttribute("Text");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
objXmlAttText.Value = dr["text"].ToString().Trim();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
objXmlElem.SetAttributeNode(objXmlAttText);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//Create Attributes CheckBox
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
XmlAttribute objXmlAttCB = objXMLDoc.CreateAttribute("CheckBox");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
objXmlAttCB.Value = "True";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
objXmlElem.SetAttributeNode(objXmlAttCB);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int id = int.Parse(dr["id"].ToString().Trim());
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
CreateXml(objXMLDoc, objXmlElem, id);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
然后用下面的方法来实现绑定
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
private void BindXmlTree()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//Create Xml File
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
XmlDocument objXmlDoc = new XmlDocument();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//Insert Xml Declaration
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
XmlDeclaration objXmlDeclare = objXmlDoc.CreateXmlDeclaration("1.0", "UTF-8", "yes");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
objXmlDoc.InsertBefore(objXmlDeclare, objXmlDoc.DocumentElement);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
XmlElement objRootElem = objXmlDoc.CreateElement("TREENODES");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
objXmlDoc.AppendChild(objRootElem);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
CreateXml(objXmlDoc, objRootElem, 0);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
objXmlDoc.Save("E:/TreeMenu.xml");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
TreeView2.TreeNodeSrc = "TreeMenu.xml";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
到最后两行我就停住了,因为问题出现了
生成的objXmlDoc文件不知道用什么方式才能绑定上TreeNodeSrc
我试了试直接 TreeView2.TreeNodeSrc = objXmlDoc;
可是TreeNodeSrc是String型的数据,没办法
有没有什么方法可以不保存这个objXmlDoc文件而直接绑定呢?
于是就换了个办法来实现
TreeView有一个节点控件叫TreeNode,直接把数据给TreeNode上
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
private void CreateTree(int belong, Microsoft.Web.UI.WebControls.TreeNode rootnode)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
DataSet ds = new DataSet();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
foreach(DataRow dr in ds.Tables[0].Rows)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Microsoft.Web.UI.WebControls.TreeNode treenode = new Microsoft.Web.UI.WebControls.TreeNode();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
treenode.Text = dr["text"].ToString().Trim();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
treenode.CheckBox = true;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
treenode.Expanded = true;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
rootnode.Nodes.Add(treenode);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int id = int.Parse(dr["id"].ToString().Trim());
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
CreateTree(id, treenode);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
用的是一样的递归思想,而且我发现这样做还比较简单一些,很多TreeNode自带的属性很方便修改
如果用Xml还要手动去添加每一个属性,麻烦
在PageLoad里触发这个方法
private void Page_Load(object sender, System.EventArgs e)
{
Microsoft.Web.UI.WebControls.TreeNode rootnode = new Microsoft.Web.UI.WebControls.TreeNode();
rootnode.Expanded = true;
TreeView2.Nodes.Add(rootnode);
CreateTree(0, rootnode);
}注意,这里为了实现递归,不得不添加一个空的根节点rootnode
这样就算大致实现了
另,数据库里的数据格式是这样的
-----------------------------------------
id text belong
1 aaaa 0
2 bbbb 0
3 cccc 0
4 dddd 1
5 eeee 2
6 ffff 4
posted on 2004-10-28 22:32 流浪的狗 阅读(15011) 评论(36) 编辑 收藏 引用 收藏至365Key 所属分类: Dotnet C#
相关文章推荐
- 利用TreeView控件动态生成无限级树
- 利用TreeView控件动态生成无限级树(转)
- TreeView控件动态生成无限级树
- 利用TreeView控件动态生成无限级树(转)
- VS2005利用TreeView控件和递归函数从数据库动态生成树状菜单
- 利用TreeView控件动态生成无限级树(续:通过绑定动态xml文件)(转)
- 利用TreeView控件动态生成无限级树(续:通过绑定动态xml文件)
- 利用TreeView控件动态生成无限极树形结构(转为了明天)
- 利用TreeView控件动态生成无限级树
- VS2005利用TreeView控件和递归函数动态生成树状菜单
- [网络收集]读取XML 利用TreeView控件动态生成无限级树
- 利用TreeView控件动态生成无限级树
- 利用TreeView控件动态生成无限级树(续:通过绑定动态xml文件)
- 利用Java反射机制和Javassist实现Java对象及其属性的动态创建生成
- 利用PHP动态生成VRML网页
- 利用注解动态生成sql
- 利用动态数组生成魔方矩阵
- 利用UrlRewrite,asp.net动态生成htm页面(补充说明)
- Hibernate利用@DynamicInsert和@DynamicUpdate生成动态SQL语句
- 利用udev在/dev下动态生成/移除设备文件