在asp.net中使用TreeView,如果是靜態(tài)的增加節點(diǎn)數據,這個(gè)很好辦,但一般情況下,TreeView是要動(dòng)態(tài)顯示菜單項的,大部分都是從XML或Access、SqlServer數據庫中加載內容,要從數據庫中讀出內容動(dòng)態(tài)增加結點(diǎn),其實(shí)也不難,比如以SQL2000的PUBS數據庫為例子,我們以樹(shù)型列表方式取出“作者”做為根結點(diǎn),再取出對應作者的作品作為子節點(diǎn),來(lái)實(shí)現動(dòng)態(tài)展開(kāi)并加載數據的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事件,是在展開(kāi)樹(shù)結點(diǎn)時(shí)發(fā)生的,這里自定義了node_populate來(lái)檢查當前結點(diǎn)的深度,如果是0,就是根結點(diǎn),于是就調用FillAuthors過(guò)程,取出所有的作者,如果深度是1,則是子節點(diǎn),調用FillTitlesForAuthors過(guò)程讀取作品信息。其中,要注意動(dòng)態(tài)建立樹(shù)結點(diǎn)的過(guò)程,如下代碼:
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的屬性來(lái)看,該節點(diǎn)可以動(dòng)態(tài)擴展。
聯(lián)系客服