treeview 和数据源绑定
前几天写了个ASP.NET程序,用到了treeview作为左边菜单,依据用户的角色来初始treeview,比较简单实用,所以现在总结一下,和博友们共享一下。
首先需要一个关系数据库表结构:就这个结构
public void inimenu(string strrole)
{
InitTree(this.TreeView_Menu, Appclass.TreeDataSet(strrole));
}
//这里Appclass.TreeDataSet(strrole)为通过用户角色strrole来初始绑定treeview对应DataSet
public static DataSet TreeDataSet(string Role)
{
![](https://www.cnblogs.com/Images/dot.gif)
![](https://www.cnblogs.com/Images/dot.gif)
![](https://www.cnblogs.com/Images/dot.gif)
}
![](/Images/OutliningIndicators/None.gif)
public bool InitTree(TreeView treeview, DataSet ds)
{
treeview.Nodes.Clear();
ds.Relations.Add("NodeRelation", ds.Tables[0].Columns["NodeId"], ds.Tables[0].Columns["ModualID"], false);
![](/Images/OutliningIndicators/InBlock.gif)
foreach (DataRow dbRow in ds.Tables[0].Rows)
{
if (dbRow.IsNull("ModualID"))
{
TreeNode newNode = CreateNode(dbRow["NodeName"].ToString(), dbRow["NodeId"].ToString(), dbRow["NodeURL"].ToString());
treeview.Nodes.Add(newNode);
SubTree(dbRow, newNode);
}
}
treeview.ExpandAll();
return true;
}
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
private void SubTree(DataRow dbRow, TreeNode node)
{
foreach (DataRow childRow in dbRow.GetChildRows("NodeRelation"))
{
TreeNode childNode = CreateNode(childRow["NodeName"].ToString(), childRow["NodeId"].ToString(), childRow["NodeURL"].ToString());
node.ChildNodes.Add(childNode);
SubTree(childRow, childNode);
}
}
![](/Images/OutliningIndicators/None.gif)
private TreeNode CreateNode(string text, string tag,string url)
{
TreeNode node = new TreeNode();
node.Text = text;
node.Value = tag;
node.NavigateUrl = url;
return node;
}
![](/Images/OutliningIndicators/None.gif)
run ------>
![](/images/cnblogs_com/tianqing/treeview.JPG)
可以了,呵呵
首先需要一个关系数据库表结构:就这个结构
NodeId | ModualID | NodeName | NodeURL |
1 | <null> | head1 | URL |
2 | <null> | head2 | URL |
3 | <null> | head3 | URL |
4 | 1 | head11 | URL |
5 | 2 | head21 | URL |
6 | 3 | head31 | URL |
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
![](https://www.cnblogs.com/Images/dot.gif)
![](https://www.cnblogs.com/Images/dot.gif)
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
run ------>
可以了,呵呵