#region 树结构填充
public bool buildTree( Microsoft.Web.UI.WebControls.TreeView TreeViewName,DataSet ds )
{
//清空所有节点
TreeViewName.Nodes.Clear();
//如果为最高级,则修改以适应ds的relation中的关系
foreach(DataRow dbRow in ds.Tables[0].Rows)
{
if(dbRow["ParentMenu"].ToString()=="0")
{
dbRow["ParentMenu"]=DBNull.Value;
}
}
ds.Relations.Add("NodeRelation", ds.Tables[0].Columns["ChildMenuID"], ds.Tables[0].Columns["ParentMenu"]);
//循环绑定父node
foreach(DataRow dbRow in ds.Tables[0].Rows)
{
if(dbRow.IsNull("ParentMenu"))
{
Microsoft.Web.UI.WebControls.TreeNode newNode = CreateNode(dbRow["MenuName"].ToString(),dbRow["NavigateUrl"].ToString());
TreeViewName.Nodes.Add(newNode);
PopulateSubTree(dbRow, newNode);
}
}
return true;
}
/**//// <summary>
/// 循环绑定子node
/// </summary>
/// <param name="dbRow">dataset的行</param>
/// <param name="node">需要添加的节点</param>
private void PopulateSubTree(DataRow dbRow, Microsoft.Web.UI.WebControls.TreeNode node)
{
foreach (DataRow childRow in dbRow.GetChildRows("NodeRelation"))
{
Microsoft.Web.UI.WebControls.TreeNode childNode = CreateNode(childRow["MenuName"].ToString(),childRow["NavigateUrl"].ToString());
node.Nodes.Add(childNode);
PopulateSubTree(childRow, childNode);
}
}
/**//// <summary>
/// 给所有node附加属性
/// </summary>
/// <param name="text">呈现文本</param>
/// <param name="imgUrl">URL</param></param>
/// <returns>返回node</returns>
private Microsoft.Web.UI.WebControls.TreeNode CreateNode(string text,string NavigateUrl)
{
Microsoft.Web.UI.WebControls.TreeNode node = new Microsoft.Web.UI.WebControls.TreeNode();
/**//*
* 其他node属性自己添加,现在只添加两个
node.ID;
node.ImageUrl;
node.Target;
node.Expanded;
node.CheckBox
* */
node.Text = text;
node.NavigateUrl=NavigateUrl;
return node;
}
#endregion