2
3if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[treetemp]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
4drop table [dbo].[treetemp]
5GO
6
7CREATE TABLE [dbo].[treetemp] (
8 [childmenuid] [int] IDENTITY (1, 1) NOT NULL ,
9 [childmenu] [varchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
10 [navigateurl] [varchar] (100) COLLATE Chinese_PRC_CI_AS NULL ,
11 [parentmenu] [int] NOT NULL ,
12 [MenuName] [varchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
13 [img] [text] COLLATE Chinese_PRC_CI_AS NULL
14) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
15GO
16
17
#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