asp.net 2.0中TREEVIEW中动态增加结点

在asp.net 2.0中,要动态从数据库中取出内容,动态增加结点,其实不难,比如以SQL SERVER 2000的PUBS数据库为例子,要以树型列表方式,取出作者,做为根结点,然后取出每位作者写过什么书,作为子结点,可以这样

1@ Page Language="C#"
1@ Import Namespace="System.Data"
1@ Import Namespace="System.Data.SqlClient"
1@ Import Namespace="System.Configuration"
  1<html xmlns=" http://www.w3.org/1999/xhtml ">
  2<head runat="server">
  3<title>Dynamic Population of the TreeView Control</title>
  4<script runat="server">
  5
  6void Node_Populate(object sender, 
  7
  8System.Web.UI.WebControls.TreeNodeEventArgs e) 
  9
 10{ 
 11
 12if(e.Node.ChildNodes.Count == 0) 
 13
 14{ 
 15
 16switch( e.Node.Depth ) 
 17
 18{ 
 19
 20case 0: 
 21
 22FillAuthors(e.Node); 
 23
 24break; 
 25
 26case 1: 
 27
 28FillTitlesForAuthors(e.Node); 
 29
 30break; 
 31
 32} 
 33
 34} 
 35
 36} 
 37
 38  
 39void FillAuthors(TreeNode node) 
 40
 41{ 
 42
 43string connString = System.Configuration.ConfigurationSettings. 
 44
 45ConnectionStrings["NorthwindConnnection"].ConnectionString; 
 46
 47SqlConnection connection = new SqlConnection(connString); 
 48
 49SqlCommand command = new SqlCommand("Select * From 
 50
 51authors",connection); 
 52
 53SqlDataAdapter adapter = new SqlDataAdapter(command); 
 54
 55DataSet authors = new DataSet(); 
 56
 57adapter.Fill(authors); 
 58
 59if (authors.Tables.Count > 0) 
 60
 61{ 
 62
 63foreach (DataRow row in authors.Tables[0].Rows) 
 64
 65{ 
 66
 67TreeNode newNode = new 
 68
 69TreeNode(row["au_fname"].ToString() + " " + 
 70
 71row["au_lname"].ToString(), 
 72
 73row["au_id"].ToString()); 
 74
 75newNode.PopulateOnDemand = true; 
 76
 77newNode.SelectAction = TreeNodeSelectAction.Expand; 
 78
 79node.ChildNodes.Add(newNode); 
 80
 81} 
 82
 83} 
 84
 85} 
 86
 87  
 88void FillTitlesForAuthors(TreeNode node) 
 89
 90{ 
 91
 92string authorID = node.Value; 
 93
 94string connString = System.Configuration.ConfigurationSettings. 
 95
 96ConnectionStrings["NorthwindConnnection"].ConnectionString; 
 97
 98SqlConnection connection = new SqlConnection(connString); 
 99
100SqlCommand command = new SqlCommand("Select T.title, 
101
102T.title_id From titles T" + 
103
104" Inner Join titleauthor TA on 
105
106T.title_id = TA.title_id " + 
107
108" Where TA.au_id = '" + authorID + "'", connection); 
109
110SqlDataAdapter adapter = new SqlDataAdapter(command); 
111
112DataSet titlesForAuthors = new DataSet(); 
113
114adapter.Fill(titlesForAuthors); 
115
116if (titlesForAuthors.Tables.Count > 0) 
117
118{ 
119
120foreach (DataRow row in titlesForAuthors.Tables[0].Rows) 
121
122{ 
123
124TreeNode newNode = new TreeNode( 
125
126row["title"].ToString(), row["title_id"].ToString()); 
127
128newNode.PopulateOnDemand = false; 
129
130newNode.SelectAction = TreeNodeSelectAction.None; 
131
132node.ChildNodes.Add(newNode); 
133
134} 
135
136} 
137
138} 
139
140  
141</script>
142</head>
143<body>
144<form id="form1" runat="server">
145<div>
146<asp:treeviewrunat="server" collapseimageurl="Images/open.gif" expandimageurl="Images/closed.gif" id="tvwauthors" ontreenodepopulate="Node_Populate">
147<nodes>
148<asp:treenodetext="authors" populateondemand="true" value="0"></asp:treenodetext="authors">
149</nodes>
150
151</asp:treeviewrunat="server"></div>
152</form>
153</body>
154</html>

其中,注意ontreenodepopulate事件,是在展开树的结点时发生的,这里定义了自定义的NODE_POPULATE,在node_populate中,检查当前结点的深度,如果是0,就是根结点,于是就调用FillAuthors过程,取出所有的作者,如果深度是1,则是叶子结点,调用FillTitlesForAuthors过程。其中,要注意它们中的动态建立树结点的过程,如:
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属性表明,该结点会动态扩展。

Published At
Categories with Web编程
Tagged with
comments powered by Disqus