/// summary>
/// 生成根节点
/// /summary>
/// param name="treeview">/param>
protected void BindTreeView(long ID, TreeView treeview)
{
DataTable dt = menuLogic.GetMenu2(ID, User.Identity.Name);
treeview.Nodes.Clear();
DataRow[] parentrow = dt.Select("par_id=0");
for (int i = 0; i parentrow.Length; i++)
{
TreeNode rootnode = new TreeNode();
rootnode.Text = parentrow[i]["node_name"].ToString(); //parentrow[i][3].ToString();
rootnode.Value = parentrow[i]["ID"].ToString(); //parentrow[i][1].ToString(); 主键
rootnode.Expanded = true;
rootnode.Selected = false;
rootnode.SelectAction = TreeNodeSelectAction.None;
treeview.Nodes.Add(rootnode);
CreateChildNode(rootnode, dt); //
}
}
/// summary>
/// 生成子节点
/// /summary>
/// param name="parentNode">/param>
/// param name="datatable">/param>
protected void CreateChildNode(TreeNode parentNode, DataTable datatable)
{
DataRow[] rowlist = datatable.Select("par_id=" + parentNode.Value);
for (int i = 0; i rowlist.Length; i++)
{
TreeNode node = new TreeNode();
if (datatable.Select("par_id=" + rowlist[i]["ID"].ToString().Trim()).Length > 0)
{
node.Text = rowlist[i]["node_name"].ToString();
node.Value = rowlist[i]["ID"].ToString();
}
else
{
node.Text = "a href=\"javascript:;\" onclick=\"OpenUrl('" + rowlist[i]["url"].ToString().Trim() + "',this)\">" + rowlist[i]["node_name"].ToString() + "/a>";
node.Value = rowlist[i]["ID"].ToString();
}
node.Expanded = false;
node.Selected = false;
node.SelectAction = TreeNodeSelectAction.None;
parentNode.ChildNodes.Add(node);
CreateChildNode(node, datatable); //递归调用
}
}
更多关于asp.net相关内容感兴趣的读者可查看本站专题:《asp.net操作json技巧总结》、《asp.net字符串操作技巧汇总》、《asp.net操作XML技巧总结》、《asp.net文件操作技巧汇总》、《asp.net ajax技巧总结专题》及《asp.net缓存操作技巧总结》。