TreeView.TreeNodePopulate 事件
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
当其 PopulateOnDemand 属性设置为 true
的节点在 TreeView 控件中展开时发生。
public:
event System::Web::UI::WebControls::TreeNodeEventHandler ^ TreeNodePopulate;
public event System.Web.UI.WebControls.TreeNodeEventHandler TreeNodePopulate;
member this.TreeNodePopulate : System.Web.UI.WebControls.TreeNodeEventHandler
Public Custom Event TreeNodePopulate As TreeNodeEventHandler
事件类型
示例
下面的代码示例演示如何使用 TreeNodePopulate 事件在服务器上动态填充 控件中的节点 TreeView 。 请注意, EnableClientScript 属性设置为 false
,以防止在客户端上处理 expanding-node 事件。
<%@ Page Language="C#" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
void PopulateNode(Object sender, TreeNodeEventArgs e)
{
// Call the appropriate method to populate a node at a particular level.
switch(e.Node.Depth)
{
case 0:
// Populate the first-level nodes.
PopulateCategories(e.Node);
break;
case 1:
// Populate the second-level nodes.
PopulateProducts(e.Node);
break;
default:
// Do nothing.
break;
}
}
void PopulateCategories(TreeNode node)
{
// Query for the product categories. These are the values
// for the second-level nodes.
DataSet ResultSet = RunQuery("Select CategoryID, CategoryName From Categories");
// Create the second-level nodes.
if(ResultSet.Tables.Count > 0)
{
// Iterate through and create a new node for each row in the query results.
// Notice that the query results are stored in the table of the DataSet.
foreach (DataRow row in ResultSet.Tables[0].Rows)
{
// Create the new node. Notice that the CategoryId is stored in the Value property
// of the node. This will make querying for items in a specific category easier when
// the third-level nodes are created.
TreeNode NewNode = new TreeNode(row["CategoryName"].ToString(), row["CategoryID"].ToString());
// Set the PopulateOnDemand property to true so that the child nodes can be
// dynamically populated.
NewNode.PopulateOnDemand = true;
// Set additional properties for the node.
NewNode.SelectAction = TreeNodeSelectAction.Expand;
// Add the new node to the ChildNodes collection of the parent node.
node.ChildNodes.Add(NewNode);
}
}
}
void PopulateProducts(TreeNode node)
{
// Query for the products of the current category. These are the values
// for the third-level nodes.
DataSet ResultSet = RunQuery("Select ProductName From Products Where CategoryID=" + node.Value);
// Create the third-level nodes.
if(ResultSet.Tables.Count > 0)
{
// Iterate through and create a new node for each row in the query results.
// Notice that the query results are stored in the table of the DataSet.
foreach (DataRow row in ResultSet.Tables[0].Rows)
{
// Create the new node.
TreeNode NewNode = new TreeNode(row["ProductName"].ToString());
// Set the PopulateOnDemand property to false because these are leaf nodes and
// do not need to be populated.
NewNode.PopulateOnDemand = false;
// Set additional properties for the node.
NewNode.SelectAction = TreeNodeSelectAction.None;
// Add the new node to the ChildNodes collection of the parent node.
node.ChildNodes.Add(NewNode);
}
}
}
DataSet RunQuery(String QueryString)
{
// Declare the connection string. This example uses Microsoft SQL Server and connects to the
// Northwind sample database.
String ConnectionString = "server=localhost;database=NorthWind;Integrated Security=SSPI";
SqlConnection DBConnection = new SqlConnection(ConnectionString);
SqlDataAdapter DBAdapter;
DataSet ResultsDataSet = new DataSet();
try
{
// Run the query and create a DataSet.
DBAdapter = new SqlDataAdapter(QueryString, DBConnection);
DBAdapter.Fill(ResultsDataSet);
// Close the database connection.
DBConnection.Close();
}
catch(Exception ex)
{
// Close the database connection if it is still open.
if(DBConnection.State == ConnectionState.Open)
{
DBConnection.Close();
}
Message.Text = "Unable to connect to the database.";
}
return ResultsDataSet;
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>TreeView TreeNodePopulate Example</title>
</head>
<body>
<form id="form1" runat="server">
<h3>TreeView TreeNodePopulate Example</h3>
<asp:TreeView id="LinksTreeView"
Font-Names= "Arial"
ForeColor="Blue"
EnableClientScript="false"
OnTreeNodePopulate="PopulateNode"
runat="server">
<Nodes>
<asp:TreeNode Text="Inventory"
SelectAction="Expand"
PopulateOnDemand="true"/>
</Nodes>
</asp:TreeView>
<br /><br />
<asp:Label id="Message" runat="server"/>
</form>
</body>
</html>
<%@ Page Language="VB" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
Sub PopulateNode(ByVal sender As Object, ByVal e As TreeNodeEventArgs)
' Call the appropriate method to populate a node at a particular level.
Select Case e.Node.Depth
Case 0
' Populate the first-level nodes.
PopulateCategories(e.Node)
Case 1
' Populate the second-level nodes.
PopulateProducts(e.Node)
Case Else
' Do nothing.
End Select
End Sub
Sub PopulateCategories(ByVal node As TreeNode)
' Query for the product categories. These are the values
' for the second-level nodes.
Dim ResultSet As DataSet = RunQuery("Select CategoryID, CategoryName From Categories")
' Create the second-level nodes.
If ResultSet.Tables.Count > 0 Then
' Iterate through and create a new node for each row in the query results.
' Notice that the query results are stored in the table of the DataSet.
Dim row As DataRow
For Each row In ResultSet.Tables(0).Rows
' Create the new node. Notice that the CategoryId is stored in the Value property
' of the node. This will make querying for items in a specific category easier when
' the third-level nodes are created.
Dim NewNode As TreeNode = New TreeNode(row("CategoryName").ToString(), row("CategoryID").ToString())
' Set the PopulateOnDemand property to true so that the child nodes can be
' dynamically populated.
NewNode.PopulateOnDemand = True
' Set additional properties for the node.
NewNode.SelectAction = TreeNodeSelectAction.Expand
' Add the new node to the ChildNodes collection of the parent node.
node.ChildNodes.Add(NewNode)
Next
End If
End Sub
Sub PopulateProducts(ByVal node As TreeNode)
' Query for the products of the current category. These are the values
' for the third-level nodes.
Dim ResultSet As DataSet = RunQuery("Select ProductName From Products Where CategoryID=" & node.Value)
' Create the third-level nodes.
If ResultSet.Tables.Count > 0 Then
' Iterate through and create a new node for each row in the query results.
' Notice that the query results are stored in the table of the DataSet.
Dim row As DataRow
For Each row In ResultSet.Tables(0).Rows
' Create the new node.
Dim NewNode As TreeNode = New TreeNode(row("ProductName").ToString())
' Set the PopulateOnDemand property to false because these are leaf nodes and
' do not need to be populated.
NewNode.PopulateOnDemand = False
' Set additional properties for the node.
NewNode.SelectAction = TreeNodeSelectAction.None
' Add the new node to the ChildNodes collection of the parent node.
node.ChildNodes.Add(NewNode)
Next
End If
End Sub
Function RunQuery(ByVal QueryString As String) As DataSet
' Declare the connection string. This example uses Microsoft SQL Server and connects to the
' Northwind sample database.
Dim ConnectionString As String = "server=localhost;database=NorthWind;Integrated Security=SSPI"
Dim DBConnection As SqlConnection = New SqlConnection(ConnectionString)
Dim DBAdapter As SqlDataAdapter
Dim ResultsDataSet As DataSet = New DataSet
Try
' Run the query and create a DataSet.
DBAdapter = New SqlDataAdapter(QueryString, DBConnection)
DBAdapter.Fill(ResultsDataSet)
' Close the database connection.
DBConnection.Close()
Catch ex As Exception
' Close the database connection if it is still open.
If DBConnection.State = ConnectionState.Open Then
DBConnection.Close()
End If
Message.Text = "Unable to connect to the database."
End Try
Return ResultsDataSet
End Function
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>TreeView TreeNodePopulate Example</title>
</head>
<body>
<form id="form1" runat="server">
<h3>TreeView TreeNodePopulate Example</h3>
<asp:TreeView id="LinksTreeView"
Font-Names= "Arial"
ForeColor="Blue"
EnableClientScript="false"
OnTreeNodePopulate="PopulateNode"
runat="server">
<Nodes>
<asp:TreeNode Text="Inventory"
SelectAction="Expand"
PopulateOnDemand="true"/>
</Nodes>
</asp:TreeView>
<br /><br />
<asp:Label id="Message" runat="server"/>
</form>
</body>
</html>
注解
有时,由于数据大小或依赖于用户输入的自定义内容,以静态方式预定义树结构是不切实际的。 因此, TreeView 控件支持动态节点填充。
PopulateOnDemand当节点的 属性设置为 true
时,将在展开节点时在运行时填充该节点。
若要动态填充节点,请先将节点的 属性设置为 PopulateOnDemandtrue
。 接下来,为 TreeNodePopulate 以编程方式填充节点的事件定义事件处理方法。 典型的事件处理方法从数据源检索节点数据,将数据放入节点结构,然后将节点结构添加到 ChildNodes 要填充的节点的集合中。 通过将 对象添加到 TreeNode 父节点的 集合来创建 ChildNodes 节点结构。
注意
PopulateOnDemand当节点的 属性设置为 true
时,必须动态填充该节点。 不能以声明方式将另一个节点嵌套在它下面;否则,页面上将发生错误。
支持 (Microsoft Internet Explorer 4.0 兼容浏览器及更高版本的浏览器) 还可以利用客户端节点填充。 启用后,这允许控件在 TreeView 扩展该节点时动态填充客户端上的节点,从而避免需要发回服务器。 有关客户端节点填充的详细信息,请参阅 PopulateNodesFromClient。
有关如何处理事件的详细信息,请参阅 处理和引发事件。