http://www.cnblogs.com/freeliver54/archive/2007/01/05/612393.html
http://www.cnblogs.com/Terrylee/archive/2006/08/01/Enterprise_Library.html
:建立数据库
我们在SQL SERVER 2000里建立一个表tbTree,表的结构设计如下:
列名 数据类型 描述 长度 主键
ID Int 节点编号 4 是
ParentID Int 父节点编号 4
ConText Nvarchar 我们要显示的节点内容 50
在SQL SERVER 2000中建表的脚本:
CREATE TABLE [dbo].[tbTree] (
[ID] [int] NOT NULL ,
[Context] [nvarchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
[ParentID] [int] NULL
) ON [PRIMARY]
在表中添加如下记录:
insert tbtree (ID,Context,ParentID) values ( 1,'中国',0)
insert tbtree (ID,Context,ParentID) values ( 2,'北京',11)
insert tbtree (ID,Context,ParentID) values ( 3,'天津',11)
insert tbtree (ID,Context,ParentID) values ( 4,'河北省',1)
insert tbtree (ID,Context,ParentID) values ( 5,'广东省',1)
insert tbtree (ID,Context,ParentID) values ( 6,'广州',5)
insert tbtree (ID,Context,ParentID) values ( 7,'四川省',1)
insert tbtree (ID,Context,ParentID) values ( 8,'成都',7)
insert tbtree (ID,Context,ParentID) values ( 9,'深圳',5)
insert tbtree (ID,Context,ParentID) values ( 10,'石家庄',4)
insert tbtree (ID,Context,ParentID) values ( 11,'辽宁省',1)
insert tbtree (ID,Context,ParentID) values ( 12,'大连',11)
insert tbtree (ID,Context,ParentID) values ( 13,'上海',1)
insert tbtree (ID,Context,ParentID) values ( 14,'天河软件园',6)
insert tbtree (ID,Context,ParentID) values ( 15,'汕头',5)
页面文件Default.aspx
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>测试</title>
<script language=javascript>
function postBackByObject()
{
var o = window.event.srcElement;
if (o.tagName == "INPUT" && o.type == "checkbox")
{
__doPostBack("","");
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TreeView ID="trvList" runat="server" OnTreeNodeCheckChanged="trvList_TreeNodeCheckChanged" >
</asp:TreeView>
</div>
</form>
</body>
</html>
代码文件Default.aspx.cs
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data.Common;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
BindDate();
}
}
protected void BindDate()
{
Database db = DatabaseFactory.CreateDatabase();
string select = "select * from tbTree";
DbCommand cmd = db.GetSqlStringCommand(select);
DataSet ds = db.ExecuteDataSet(cmd);
LoadTreeView(0, (TreeNode)null,ds);
trvList.Attributes.Add("onclick", "postBackByObject()");
}
protected void LoadTreeView(int ParentID, TreeNode pNode,DataSet ds)
{
DataView dvTree = new DataView(ds.Tables[0]);
dvTree.RowFilter = "[ParentID]=" + ParentID;
foreach (DataRowView Row in dvTree)
{
if (pNode == null)
{
//添加根节点
TreeNode tn = new TreeNode();
tn.Text = Row["Context"].ToString();
tn.Value = Row["ID"].ToString();
tn.Expanded = true;
tn.SelectAction = TreeNodeSelectAction.Expand;
tn.ShowCheckBox = true;
trvList.Nodes.Add(tn);
dvTree = null;
LoadTreeView(Int32.Parse(Row["ID"].ToString()), tn,ds); //再次递归
}
else
{
TreeNode tnmore = new TreeNode();
tnmore.Text = Row["Context"].ToString();
tnmore.Value = Row["Context"].ToString();
tnmore.Expanded = false;
tnmore.ShowCheckBox = true;
tnmore.SelectAction = TreeNodeSelectAction.Expand;
pNode.ChildNodes.Add(tnmore);
dvTree = null;
LoadTreeView(Int32.Parse(Row["ID"].ToString()), tnmore,ds); //再次递归
}
}
}
protected void trvList_TreeNodeCheckChanged(object sender, TreeNodeEventArgs e)
{
SetChildChecked(e.Node);
}
protected void SetChildChecked(TreeNode parentNode)
{
foreach (TreeNode node in parentNode.ChildNodes)
{
node.Checked = parentNode.Checked;
if (node.ChildNodes.Count > 0)
{
SetChildChecked(node);
}
}
}
}
web.config中添加
<section name="dataConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null"/>
</configSections>
<dataConfiguration defaultDatabase="ConnStr"/>
<connectionStrings>
<add name="ConnStr" connectionString="你的连接字符串" providerName="System.Data.SqlClient"/>
</connectionStrings>