今天上班的时候需要用到TreeView,突发奇想能不能给Asp.Net Forum的后台管理做一个树形的管理菜单呢?呵呵~~~,建议大家边听Metallica边写代码,比较有灵感哦..
第一步:建表如下
MenuNodeID
|
ParentNodeID
|
NodeText
|
LinkFile
|
HotDegree
|
Expanded
|
Int4autoseed
|
int
|
nVarChar
|
nVarChar
|
Int
|
bit
|
其他字段不用说了,其中HotDegree是一个记录该菜单的点击次数的字段,TM Messager不是新增加了一个热度吗?我们也加一个。Expanded字段设定该节点是否默认展开。然后增加几个存储过程。另外需要在表中增加一条默认的数据,不增加应该也可以的,在查询分析器中运行以下SQL代码,在项目的存储过程中作为存储过程加入后在Vs中运行也是一样的:
1![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
/**//*================================================
2
--Install_Data_ManageMenu
3
=================================================*/
4
SET IDENTITY_INSERT ManageMenu ON
5
print '创建Menu默认数据'
6
IF EXISTS (SELECT * FROM ManageMenu WHERE NodeParentID = 0)
7
DELETE ManageMenu WHERE NodeParentID = 0
8![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
9
INSERT INTO
10
ManageMenu
11
(
12
MenuNodeID,NodeParentID,NodeText,LinkPath,HotDegree,ExPanded
13
)
14
VALUES
15
(
16
0,-1,'top','#',0,0
17
)
18
SET IDENTITY_INSERT ManageMenu OFF
19![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
20
--如果是在Vs中运行的化把GO去掉
21
GO
第二步:在Components/Components下增加Menu.cs如下:
1
public class Menu
2![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
3
private int nodeID;
4![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
5![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
6
public int NodeID
7![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
8![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{return nodeID;}
9![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{nodeID = value;}
10
}
11
}
在Components下增加Menus.cs如下:
public class Menus
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
public static ArrayList GetParentNode()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
AnalyseDataProvider dp = AnalyseDataProvider.Instance();
return dp.GetParentNodes();
}
public static ArrayList GetChildNodes(int childnodeid)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
AnalyseDataProvider dp = AnalyseDataProvider.Instance();
return dp.GetChildNodes(childnodeid);
}
public static void UpdateHotDegree(Menu menu)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
menu.HotDegree = menu.HotDegree + 1;
AnalyseDataProvider dp = AnalyseDataProvider.Instance();
UpdateMenu(menu);
}
}
第三步:在ForumsDataProvider.cs中增加抽象方法
1
public static Components.Menu PopulateMenu(IDataReader reader)
2![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
3
Components.Menu menu = new Menu();
4
menu.NodeID = (int)reader["MenuNodeID"];
5
menu.ParentID = (int)reader["NodeParentID"];
6
menu.Text = (string)reader["NodeText"];
7
menu.LinkPath = (string)reader["LinkPath"];
8
menu.HotDegree = (int)reader["HotDegree"];
9
menu.ExPanded = (bool)reader["ExPanded"];
10
return menu;
11
}
12
public abstract ArrayList GetParentNodes();
13
public abstract ArrayList GetChildNodes(int childnodeid);
14
public abstract int CreateUpdateDeleteMenu(Menu menu,DataProviderAction action);
第四步:在SqlDataProvider项目中的SqlDataProvider.cs中重写以上方法,这里再不赘述了。
第五步:在Web项目中增加ViewManageMenu.ascx控件,并引用IE WebControls控件
第六步:在Controls项目中增加ManageMenu.cs,大致分以下几部分:
1.装载皮肤样式,写Asp.Net Forums换肤的文章很多,大家可以参考,如果没有注意到那么可以到这里去看看
http://ode.cnblogs.com/archive/2005/11/06/270262.html
2.创建单个节点,写的比较乱,将就看吧,呵呵
1![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
/**//// <summary>
2
/// 创建节点
3
/// </summary>
4
/// <param name="text">该节点的Text</param>
5
/// <param name="linkpath">该节点的链接路径(相对)</param>
6
/// <param name="expanded">是否默认展开</param>
7
/// <returns>节点</returns> 8
private TreeViewNode CreateNode(string text,string linkpath,bool expanded)
9![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
10
//定义一个新的节点
11
TreeViewNode node = new TreeViewNode();
12
//设置该节点的属性
13
node.Text = text;
14
node.NavigateUrl = (linkpath == "#")?"#":Globals.ApplicationPath + "/" +linkpath;
15
node.Expanded = expanded;
16
//返回该节点
17
return node;
18
}
3.递归树
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
/**//// <summary>
/// 递归树
/// </summary>
/// <param name="menu">Menu</param>
/// <param name="node">当前节点</param>
private void PopulateSubTree(Components.Menu menu, TreeViewNode node)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
foreach(Components.Menu childmenu in Menus.GetChildNodes(menu.NodeID))
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
TreeViewNode childnode = CreateNode(childmenu.Text,childmenu.LinkPath,childmenu.ExPanded);
node.Nodes.Add(childnode);
PopulateSubTree(childmenu, childnode);
}
}
4.绑定方法
ArrayList menus;
TreeViewNode treeviewnode;
menus = Menus.GetParentNode();
foreach(AnalyseOde.Components.Menu menu in menus)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
treeviewnode = CreateNode(menu.Text,menu.LinkPath,menu.ExPanded);
treeview.Nodes.Add(treeviewnode);
PopulateSubTree(menu, treeviewnode);
if(menu.ParentID == -1)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
break;
}
}
5.OK了。
第七步:把ViewManageMenu.ascx放入AdminMasterPage.ascx,完工。
这样做没有什么好处,只是一个小尝试罢了。
因为这些东西不是写在Asp.Net Forum中的,所以代码就无法提供了,还望谅解。