创建表及插入模拟数据
CREATE TABLE [dbo][sysMenuTree](
[NoteId] [decimal]( ) NOT NULL
[ParentId] [decimal]( ) NULL
[sText] [nvarchar]() NULL
[sValue] [nvarchar]() NULL
[sURL] [nvarchar]() NULL
[sTarget] [nvarchar]() NULL
[Chger] [nvarchar]() NULL
[ChgTime] [nvarchar]() NULL
CONSTRAINT [PK_sysMenuTree] PRIMARY KEY CLUSTERED
(
[NoteId] ASC
)WITH (PAD_INDEX = OFF STATISTICS_NORECOMPUTE = OFF IGNORE_DUP_KEY = OFF ALLOW_ROW_LOCKS = ON ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
insert into sysMenuTree values(N目录N目录)
insert into sysMenuTree values(N目录N目录)
insert into sysMenuTree values(N目录N目录)
insert into sysMenuTree values(N项目N项目)
insert into sysMenuTree values(N项目N项目)
insert into sysMenuTree values(N项目N项目)
insert into sysMenuTree values(N项目N项目)
insert into sysMenuTree values(N项目N项目)
insert into sysMenuTree values(N项目N项目)
insert into sysMenuTree values(N项目N项目)
在ASPNET中读取数据绑定到TreeView实现代码
<%@ Page Language=C# AutoEventWireup=true CodeFile=Defaultaspxcs Inherits=_Default %>
<!DOCTYPE html PUBLIC //WC//DTD XHTML Transitional//EN transitionaldtd>
<html xmlns=>
<head runat=server>
<title></title>
</head>
<body>
<form id=form runat=server>
<div>
<asp:TreeView ID=treeMenu runat=server>
</asp:TreeView>
</div>
</form>
</body>
</html>
using System;
using SystemWebUIWebControls;
using SystemData;
using SystemDataSqlClient;
public partial class _Default : SystemWebUIPage
{
private readonly string ConnString = @server=\MSSQLSERVER;database=chart;uid=sa;pwd=;
private DataTable dt = null;
protected void Page_Load(object sender EventArgs e)
{
if(!IsPostBack)
{
dt = new DataTable();
GetMenuToDataTable(select * from sysMenuTreedt);
BindTree(dtnull);
}
}
private void BindTree(DataTable dtSourceTreeNode parentNodestring parentID)
{
DataRow[] rows = dtSourceSelect(stringFormat(ParentID={}parentID));
foreach(DataRow row in rows)
{
TreeNode node = new TreeNode();
nodeText = row[sText]ToString();
nodeValue = row[sValue]ToString();
BindTree(dtSourcenoderow[NoteId]ToString());
if(parentNode == null)
{
treeMenuNodesAdd(node);
}
else
{
parentNodeChildNodesAdd(node);
}
}
}
private DataTable GetMenuToDataTable(string queryDataTable dt)
{
using(SqlConnection conn = new SqlConnection(ConnString))
{
SqlCommand cmd = new SqlCommand(queryconn);
SqlDataAdapter ada = new SqlDataAdapter(cmd);
adaFill(dt);
}
return dt;
}
}