在asp.net中使用TreeView,如果是靜態(tài)的增加節(jié)點(diǎn)數(shù)據(jù),這個(gè)很好辦,但一般情況下,TreeView是要?jiǎng)討B(tài)顯示菜單項(xiàng)的,大部分都是從XML或Access、SqlServer數(shù)據(jù)庫(kù)中加載內(nèi)容,要從數(shù)據(jù)庫(kù)中讀出內(nèi)容動(dòng)態(tài)增加結(jié)點(diǎn),其實(shí)也不難,比如以SQL2000的PUBS數(shù)據(jù)庫(kù)為例子,我們以樹型列表方式取出“作者”做為根結(jié)點(diǎn),再取出對(duì)應(yīng)作者的作品作為子節(jié)點(diǎn),來實(shí)現(xiàn)動(dòng)態(tài)展開并加載數(shù)據(jù)的TreeView,我們可以這樣做:
<%@ Page Language="C#"%><%@ Import Namespace="System.Data"%><%@ Import Namespace="System.Data.SqlClient"%><%@ Import Namespace="System.Configuration"%><!DOCTYPE htmlPUBLIC"-//W3C//DTD XHTML 1.1//EN""http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head runat="server"><title>Dynamic Population of the TreeView Control</title><script runat=server>void Node_Populate(object sender,System.Web.UI.WebControls.TreeNodeEventArgs e){if(e.Node.ChildNodes.Count == 0){switch( e.Node.Depth ){case 0:FillAuthors(e.Node);break;case 1:FillTitlesForAuthors(e.Node);break;}}}void FillAuthors(TreeNode node){string connString = System.Configuration.ConfigurationSettings.ConnectionStrings["NorthwindConnnection"].ConnectionString;SqlConnection connection = new SqlConnection(connString);SqlCommand command = new SqlCommand("Select * Fromauthors",connection);SqlDataAdapter adapter = new SqlDataAdapter(command);DataSet authors = new DataSet();adapter.Fill(authors);if (authors.Tables.Count > 0){foreach (DataRow row in authors.Tables[0].Rows){TreeNode newNode = newTreeNode(row["au_fname"].ToString() + " " +row["au_lname"].ToString(),row["au_id"].ToString());newNode.PopulateOnDemand = true;newNode.SelectAction = TreeNodeSelectAction.Expand;node.ChildNodes.Add(newNode);}}}void FillTitlesForAuthors(TreeNode node){string authorID = node.Value;string connString = System.Configuration.ConfigurationSettings.ConnectionStrings["NorthwindConnnection"].ConnectionString;SqlConnection connection = new SqlConnection(connString);SqlCommand command = new SqlCommand("Select T.title,T.title_id From titles T" +" Inner Join titleauthor TA on
T.title_id = TA.title_id " +
" Where TA.au_id = '" + authorID + "'", connection);
SqlDataAdapter adapter = new SqlDataAdapter(command);
DataSet titlesForAuthors = new DataSet();
adapter.Fill(titlesForAuthors);
if (titlesForAuthors.Tables.Count > 0)
{
foreach (DataRow row in titlesForAuthors.Tables[0].Rows)
{
TreeNode newNode = new TreeNode(
row["title"].ToString(), row["title_id"].ToString());
newNode.PopulateOnDemand = false;
newNode.SelectAction = TreeNodeSelectAction.None;
node.ChildNodes.Add(newNode);
}
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TreeViewRunat="Server" ExpandImageUrl="Images/closed.gif"
CollapseImageUrl="Images/open.gif"
OnTreeNodePopulate="Node_Populate" ID="tvwauthors">
<Nodes>
<asp:TreeNodeText="Authors" PopulateOnDemand=true
Value="0"/>
</Nodes>
</asp:TreeView>
</div>
</form>
</body>
</html>
其中,要注意ontreenodepopulate事件,是在展開樹結(jié)點(diǎn)時(shí)發(fā)生的,這里自定義了node_populate來檢查當(dāng)前結(jié)點(diǎn)的深度,如果是0,就是根結(jié)點(diǎn),于是就調(diào)用FillAuthors過程,取出所有的作者,如果深度是1,則是子節(jié)點(diǎn),調(diào)用FillTitlesForAuthors過程讀取作品信息。其中,要注意動(dòng)態(tài)建立樹結(jié)點(diǎn)的過程,如下代碼:
TreeNode newNode = new TreeNode(row["au_fname"].ToString() + " " +
row["au_lname"].ToString(),
row["au_id"].ToString());
newNode.PopulateOnDemand = true;
newNode.SelectAction = TreeNodeSelectAction.Expand;
node.ChildNodes.Add(newNode);
從popluateondemand的屬性來看,該節(jié)點(diǎn)可以動(dòng)態(tài)擴(kuò)展。
聯(lián)系客服