public class Tree
{
public void
BindTree(TreeView treeView)
{
Tree tree = new Tree();
DataTable dataTable = tree.GetTrees();
treeView.Nodes.Clear();
// 清空树
TreeNode rootnode = new TreeNode();
rootnode.Text = "办公平台";
// 给节点绑定显示值
rootnode.NodeData = "0";
// 给节点绑定key值
rootnode.Expanded = false;
// 默认根结点为展开
rootnode.NavigateUrl = "OfficeIndex.aspx";
rootnode.Target = "MainFrame";
rootnode.ImageUrl = "../Images/Icon/05.gif";
treeView.Nodes.Add(rootnode);
CreateChildNode(rootnode,dataTable);
// 加入所有根结点以下的结点
}
public void CreateChildNode(TreeNode parentnode,DataTable dataTable)
{
DataRow[] rowList = dataTable.Select("ParentID='" + parentnode.NodeData + "'");
foreach(DataRow row in rowList)
{
TreeNode tempnode = new TreeNode();
tempnode.Text = row["Title"].ToString();
tempnode.NodeData = row["TreeID"].ToString();
tempnode.Expanded = false;
tempnode.NavigateUrl = row["Url"].ToString();
tempnode.Target = "MainFrame";
parentnode.Nodes.Add(tempnode);
CreateChildNode(tempnode,dataTable);
}
}
public DataTable GetTrees()
{
Database db = new Database();
SqlDataReader dr = null;
db.RunProc("Pr_GetTrees",out dr);
///获取DataTable对象
DataTable dt = SystemTools.ConvertDataReaderToDataTable(dr);
return(dt);
}
}