具体方法是:创建一个数据库,设计树图信息表s_menu,包含nodeid、parentid、nodename、address、icon字段,其它字段根据实际业务而定,节点名称nodename将在树型控件的节点上显示,nodeid字段保存节点的唯一标识号,parentid表示当前节点的父节点号,标识号组成了一个“链表”,记录了树上节点的结构。设计一个web窗体其上放置treeview控件,修改其属性id为tvmenu。
数据结构如下:
create table [dbo].[s_menu] (
[nodeid] [char] (6) collate chinese_prc_ci_as null ,
[parentid] [char] (6) collate chinese_prc_ci_as null ,
[nodename] [nvarchar] (50) collate chinese_prc_ci_as null ,
[address] [nvarchar] (50) collate chinese_prc_ci_as null ,
[icon] [nvarchar] (50) collate chinese_prc_ci_as null
) on [primary]
go
数据库如下:
nodeid parentid nodename address icon
100000 0 公共查询部 icon_document.gif
100001 100000 人民币日报查询 public/a1.aspx icon_settings.gif
100002 100000 外币日报查询 public/a2.aspx icon_settings.gif
100003 0 分行科技部 icon_document.gif
100004 100003 人民币日报查询 tech/a1.aspx icon_settings.gif
100005 100003 外币日报查询 tech/a2.aspx icon_settings.gif
100006 0 福田支行 icon_document.gif
100007 100006 月存款进度表 a1.aspx icon_settings.gif
100008 100006 月存款走势图 a2.aspx icon_settings.gif
100009 0 罗湖支行 icon_document.gif
100010 100009 月存款进度表 a1.aspx icon_settings.gif
100011 100009 月存款走势图 a2.aspx icon_settings.gif
menu_left.aspx文件如下:
<%@ register tagprefix=”iewc” namespace=”microsoft.web.ui.webcontrols” assembly=”microsoft.web.ui.webcontrols, version=1.0.2.226, culture=neutral, publickeytoken=31bf3856ad364e35″ %>
<%@ page language=”c#” codebehind=”menu_left.aspx.cs” autoeventwireup=”false” inherits=”hzquery.menu.menu_left” %>
<html>
<head>
<title>menu_left</title>
<meta name=”generator” content=”microsoft visual studio 7.0″>
<meta name=”code_language” content=”c#”>
<meta name=”vs_defaultclientscript” content=”javascript”>
<meta name=”vs_targetschema” content=”http://schemas.microsoft.com/intellisense/ie5″>
</head>
<body ms_positioning=”gridlayout”>
<form id=”menu_left” method=”post” runat=”server”>
<iewc:treeview id=”tvmenu” runat=”server”></iewc:treeview>
</form>
</body>
</html>
codebehind代码如下:
using system;
using system.collections;
using system.componentmodel;
using system.data;
using system.drawing;
using system.web;
using system.web.sessionstate;
using system.web.ui;
using system.web.ui.webcontrols;
using system.web.ui.htmlcontrols;
using microsoft.web.ui.webcontrols;
using system.data.sqlclient;
namespace hzquery.menu
{
/// <summary>
/// menu_left 的摘要说明。
/// </summary>
public class menu_left : system.web.ui.page
{
protected microsoft.web.ui.webcontrols.treeview tvmenu;
sqlconnection conn;
sqldataadapter mycmd;
dataset ds;
string cmdselect;
private void page_load(object sender, system.eventargs e)
{
conn=new sqlconnection(application[“connstring”].tostring());
createdataset();
inittree(tvmenu.nodes,”0″);
}
//建立数据集
private dataset createdataset()
{
cmdselect=”select * from s_menu”;
mycmd=new sqldataadapter(cmdselect,conn);
ds=new dataset();
mycmd.fill(ds,”tree”);
return ds;
}
//建树的基本思路是:从根节点开始递归调用显示子树
private void inittree(treenodecollection nds,string parentid)
{
dataview dv=new dataview();
treenode tmpnd;
string intid;
dv.table=ds.tables[“tree”];
dv.rowfilter=”parentid=”” + parentid + “”” ;
foreach(datarowview drv in dv)
{
tmpnd=new treenode();
tmpnd.id=drv[“nodeid”].tostring();
tmpnd.text=drv[“nodename”].tostring();
tmpnd.imageurl=”../images/”+drv[“icon”].tostring();
tmpnd.navigateurl=”../”+drv[“address”].tostring();
nds.add(tmpnd);
intid=drv[“parentid”].tostring();
inittree(tmpnd.nodes,tmpnd.id);
}
}
#region web form designer generated code
override protected void oninit(eventargs e)
{
initializecomponent();
base.oninit(e);
}
private void initializecomponent()
{
this.load += new system.eventhandler(this.page_load);
}
#endregion
}
}