# TreeNode.PopulateOnDemand Eigenschaft

## Definition

Ruft einen Wert ab, der angibt, ob der Knoten dynamisch aufgefüllt wird, oder legt diesen Wert fest.Gets or sets a value that indicates whether the node is populated dynamically.

``````public:
property bool PopulateOnDemand { bool get(); void set(bool value); };``````
``public bool PopulateOnDemand { get; set; }``
``member this.PopulateOnDemand : bool with get, set``
``Public Property PopulateOnDemand As Boolean``

#### Eigenschaftswert

Boolean

`true`, um den Knoten dynamisch aufzufüllen, andernfalls `false`.`true` to populate the node dynamically; otherwise, `false`. Der Standardwert ist `false`.The default is `false`.

## Beispiele

Im folgenden Codebeispiel wird veranschaulicht, wie die- PopulateOnDemand Eigenschaft verwendet wird, um anzugeben, dass ein Knoten dynamisch aufgefüllt wird.The following code example demonstrates how to use the PopulateOnDemand property to indicate that a node is populated dynamically.

``````
<%@ 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();
newNode.Text = row["CategoryName"].ToString();
newNode.Value = 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.

}

}

}

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.

}

}

}

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);
DataSet ResultsDataSet = new DataSet();

try
{

// Run the query and create a DataSet.

// 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" >
<title>TreeView PopulateNodesFromClient Example</title>
<body>
<form id="form1" runat="server">

<h3>TreeView PopulateNodesFromClient Example</h3>

Font-Names= "Arial"
ForeColor="Blue"
EnableClientScript="true"
PopulateNodesFromClient="true"
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()
Newnode.Text = row("CategoryName").ToString()
Newnode.Value = 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.

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.

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 ResultsDataSet As DataSet = New DataSet

Try

' Run the query and create a DataSet.

' 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" >
<title>TreeView PopulateNodesFromClient Example</title>
<body>
<form id="form1" runat="server">

<h3>TreeView PopulateNodesFromClient Example</h3>

Font-Names= "Arial"
ForeColor="Blue"
EnableClientScript="true"
PopulateNodesFromClient="true"
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>

``````

## Hinweise

In manchen Fällen ist es nicht praktikabel, die Struktur durch die Datengröße oder den benutzerdefinierten Inhalt, der von Benutzereingaben abhängt, statisch vorzudefinieren.Sometimes, it is not practical to statically predefine the tree structure due to data size or custom content that depends on user input. Aus diesem Grund unterstützt das-Steuerelement die TreeView dynamische Knoten Auffüllung.Because of this, the TreeView control supports dynamic node population. Wenn die-Eigenschaft eines Knotens PopulateOnDemand auf festgelegt ist `true` , wird dieser Knoten zur Laufzeit durch ein Post Back Ereignis aufgefüllt, wenn der Knoten erweitert wird.When a node's PopulateOnDemand property is set to `true`, that node is populated at run time through a postback event when the node is expanded. Um einen Knoten dynamisch aufzufüllen, muss eine Ereignis Behandlungsmethode für das-Ereignis definiert werden, die den Knoten füllt TreeNodePopulate .To populate a node dynamically, an event-handling method that populates the node must be defined for the TreeNodePopulate event.

Unterstützte Browser können auch die Client seitige Knoten Population nutzen.Supported browsers can also take advantage of client-side node population. Wenn diese Option aktiviert ist, kann das TreeView Steuerelement einen Knoten auf dem Client dynamisch auffüllen, wenn dieser Knoten erweitert wird. Dadurch wird verhindert, dass ein Postback an den Server erfolgt.When enabled, this allows the TreeView control to dynamically populate a node on the client when that node is expanded, which prevents the need to post back to the server. Weitere Informationen zur Client seitigen Knoten Population finden Sie unter PopulateNodesFromClient .For more information on client-side node population, see PopulateNodesFromClient.

Der Wert dieser Eigenschaft wird im Ansichts Zustand gespeichert.The value of this property is stored in view state.